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
BigIntegerclass 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
BigIntegerclass.
import java.math.BigInteger;
- On lines 5 and 6 we create two
BigIntegerobjects:val1with value99andval2with value100.
BigInteger val1 = new BigInteger("99");
BigInteger val2 = new BigInteger("100");
- On line 7 we call the
maxmethod on theval1object withval2as an argument. This returns theval2object as result becauseval2 > val1.
BigInteger result = val1.max(val2); //val2 - 100