This post shows how to multiply BigIntegers in java with examples. use multiplication(*) operator for multiply biginteger in java.
You can also check my previous posts on the BigInteger
class in java.
- BigInteger Class tutorials
- Convert BigInteger to/from BigDecimal
- Convert BigDecimal to/from String
- BigInteger Divide example
- Convert BigDecimal to float
- Convert BigDecimal to double
- Top 10 BigInteger Examples(/2018/09/best-10-javamathbiginteger-class.html)
- Rounding bigdecimal to 2 decimal places
- Check BigDecimal contains Zero or not
- Convert BigInteger to/from ByteArray
In java, long or integers can be multiplied using the multiplication operator
*
.
The same does not work for BigInteger objects. It gives compilation error Operator ‘*’ cannot be applied to ‘java.math.BigInteger’, ‘java.math.BigInteger’.
Here is an example using the multiplication operator
import java.math.BigInteger;
public class BigIntegerMultiply {
public static void main(String[] args) {
BigInteger operand1= BigInteger.valueOf(2);
BigInteger operand2= BigInteger.valueOf(2);
BigInteger result=operand1*operand2;
System.out.println(result);
}
}
It provides a multiply
method to do multiplication on BigInteger.
Java BigInteger multiply method with example
The multiply
method returns the result of the multiplication of values.
BigInteger is immutable and the result of methods always returns a new object.
It internally uses an array of integers to do the process, The performance is less compared with Integer multiplication.
Syntax:
public BigInteger multiply(BigInteger val)
import java.math.BigInteger;
public class BigIntegerMultiply {
public static void main(String[] args) {
BigInteger operand1= BigInteger.valueOf(212312);
BigInteger operand2= BigInteger.valueOf(2123);
BigInteger result=operand1.multiply(operand2);
System.out.println(result);
}
}
Output:
450738376