What is the BigInteger.max method in Java?

The max method of the BigInteger class returns the maximum of the current object and the passed argument.

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

Syntax

public BigInteger max(BigInteger val)

Parameter

The max method takes a BitInteger object as a parameter.

Return value

  • If the parameter passed is greater than the current object value, then the parameter is returned. Otherwise, the current object is returned.

  • If the parameter and current object are equal, then either one of them is returned.

Code

The example below demonstrates how to use the max method.

import java.math.BigInteger;
class BigIntegerMaxExample {
public static void main( String args[] ) {
BigInteger val1 = new BigInteger("99");
BigInteger val2 = new BigInteger("100");
BigInteger result = val1.max(val2);
System.out.println(result);
}
}

Explanation

In the code above,

  • On line 1 we import the BigInteger class.
import java.math.BigInteger;
  • On lines 5 and 6 we create two BigInteger objects: val1 with value 99 and val2 with value 100.
BigInteger val1 = new BigInteger("99");
BigInteger val2 = new BigInteger("100");
  • On line 7 we call the max method on the val1 object with val2 as an argument. This returns the val2 object as result because val2 > val1.
BigInteger result = val1.max(val2); //val2 - 100

Free Resources