What is the BigInteger.multiply method in Java?
In Java, the BigInteger class deals with very large integer values that the primitive data type can not represent.
The multiply method of the BigInteger class multiplies the passed BigInteger object value with the called BigInteger object value.
Syntax
public BigInteger multiply(BigInteger val)
Parameter
val: This method takes aBitIntegerobject as an argument that is to be multiplied.
Return value
This method returns a BitInteger object. The value of the returned BigInteger object is the multiplication of the current BigInteger value that calls the method and the passed argument value.
Code
The example below demonstrates the use of the multiply method:
import java.math.BigInteger;class BigIntegerMultiplyExample {public static void main( String args[] ) {BigInteger val1 = new BigInteger("123123");BigInteger val2 = new BigInteger("239857239562");BigInteger result = val1.multiply(val2);System.out.println(result);}}
Explanation
In the code above,
- In line number 1, we import the
BigIntegerclass.
import java.math.BigInteger;
- In lines number 5 and 6, we create two
BigIntegerobjectsval1with the value123123andval2with239857239562.
BigInteger val1 = new BigInteger("123123");
BigInteger val2 = new BigInteger("239857239562");
- In line number 7, we call the
multiplymethod on theval1object withval2as an argument. This method call will return aBigInteger, which has a value equal to the product ofval1andval2.
BigInteger result = val1.multiply(val2); //29531942906592126