What is the BigInteger.divide method in Java?

The divide method of the BigInteger class can divide the current BigInteger object value by the passed BigInteger value.

In Java, the BigInteger class handles vast integer mathematical operations outside the limits of all primitive types.

Syntax

public BigInteger divide(BigInteger val)

Parameters

This method takes a BigInteger object as a parameter.

Return value

The divide method returns a BigInteger object. The value of the returned BigInteger object is the division of the current BigInteger value by the passed argument value.

Code

The example below demonstrates how to use the divide method:

import java.math.BigInteger;
class BigIntegerDivideExample {
public static void main( String args[] ) {
BigInteger val1 = new BigInteger("246246");
BigInteger val2 = new BigInteger("2");
BigInteger result = val1.divide(val2);
System.out.println(result);
}
}

Explanation

  • In line 1 from the code above, we import the BigInteger class.
import java.math.BigInteger;
  • In lines 5 and 6, we create two BigInteger objects, val1 with value 246246 and val2 with value 2.
BigInteger val1 = new BigInteger("246246");
BigInteger val2 = new BigInteger("2");
  • In line 7, we call the divide method on the val1 object with val2 as an argument. This method call will return a BigInteger that has a value equal to the division of val1 by val2.
BigInteger result = val1.divide(val2); //123123

Free Resources