java.math.BigInteger max(BigInteger val)


On this document we will be showing a java example on how to use the max(BigInteger val) method of BigInteger Class. Basically this method provides mechanism to get which is numerically higher between this BigInteger and the val method argument. Whichever is higher will be returned by this method.

There is a similar method available on BigInteger class which is the compareTo() method. The max method is a little bit different because on the compareTo() method we have to interpret the result while on the max method, the biggest BigInteger is already returned.

See also:

Method Syntax

public BigInteger max(BigInteger val)

Method Argument

Data Type Parameter Description
BigInteger val value with which the maximum is to be computed.

Method Returns

The max(BigInteger val) method returns the BigInteger whose value is the greater between this BigInteger and val. If they are equal, either may be returned.


Requires Java 1.1 and up

Java BigInteger max(BigInteger val) Example

Below is a java code demonstrates the use of max(BigInteger val) method of BigInteger class. The example presented might be simple however it shows the behavior of the max() method.


import java.math.BigInteger;
import java.util.Scanner;

 * A java example source code to demonstrate
 * the use of max() method of BigInteger class

public class BigIntegerMaxExample {

	public static void main(String[] args) {	
		// ask for two input
		System.out.print("Enter the first number:");
		Scanner s = new Scanner(;
		String firstInput = s.nextLine();
		System.out.print("Enter the second number:");
		String secondInput = s.nextLine();
		// convert the string input to BigInteger
		BigInteger val1 = new BigInteger(firstInput);
		BigInteger val2 = new BigInteger(secondInput);
		// get the maximum value between the two BigInteger
		BigInteger result = val1.max(val2);
		System.out.println("The result is "+result);


This example is a lot simpler than it looks. We simply ask the user for two inputs and convert these into BigInteger. We then get and print what number is higher between the two inputs.

Sample Output

Below is the sample output when you run the above example.

BigInteger max() example output