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
BigIntegerclass 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
BigIntegerclass.
import java.math.BigInteger;
- In lines 5 and 6, we create two
BigIntegerobjects,val1with value246246andval2with value2.
BigInteger val1 = new BigInteger("246246");
BigInteger val2 = new BigInteger("2");
- In line 7, we call the
dividemethod on theval1object withval2as an argument. This method call will return aBigIntegerthat has a value equal to the division ofval1byval2.
BigInteger result = val1.divide(val2); //123123