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 a BitInteger object 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 BigInteger class.
import java.math.BigInteger;
  • In lines number 5 and 6, we create two BigInteger objects val1 with the value 123123 and val2 with 239857239562.
BigInteger val1 = new BigInteger("123123");
BigInteger val2 = new BigInteger("239857239562");
  • In line number 7, we call the multiply method on the val1 object with val2 as an argument. This method call will return a BigInteger, which has a value equal to the product of val1 and val2.
BigInteger result = val1.multiply(val2); //29531942906592126

Free Resources