BigInteger multiply method in Java with examples
- Admin
- Mar 10, 2024
- Java-examples
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.
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)
This method called on the BigInteger object, takes the same type of parameter. Returns a new BigInteger object.
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