BigInteger max() and min() Methods in Java - GeeksforGeeks

04 Dec, 2018
Prerequisite: BigInteger Basics
BigInteger max() method: The max() method of the BigInteger returns the BigInteger whose value is the greater between current BigInteger and BigInteger passed as a parameter to the method. If both the values are equal, either may be returned.There is a similar method compareTo() available on BigInteger class. The max() method is different than compareTo() in a way that in the compareTo() method we have to interpret the result while on the max method, the biggest BigInteger is already returned.Syntax:public BigInteger max(BigInteger val)Parameters: The method accepts one parameter val which refers to the value with which the maximum is to be computed.Return Value: The method returns the BigInteger whose value is the greater of this and val. If they are equal, either may be returned.Below program illustrate max() method of BigInteger class./* *Program Demonstrate max() method of BigInteger */import java.math.*;  public class GFG {     public static void main(String[] args) {                 // Create 2 BigInteger objects      BigInteger biginteger=new BigInteger("8976543245");      BigInteger val=new BigInteger("9248040402");              // Call max() method to find greater value      // between two BigIntegers.      BigInteger biggerInteger = biginteger.max(val);        String result = "Bigger Integer between "+biginteger+" and "                      +val+ " is " +biggerInteger;        // Prints the result       System.out.println(result);        }}Output:Bigger Integer between 8976543245 and 9248040402 is 9248040402

There is a similar method compareTo() available on BigInteger class. The max() method is different than compareTo() in a way that in the compareTo() method we have to interpret the result while on the max method, the biggest BigInteger is already returned.
Syntax:
public BigInteger max(BigInteger val)
Parameters: The method accepts one parameter val which refers to the value with which the maximum is to be computed.
Return Value: The method returns the BigInteger whose value is the greater of this and val. If they are equal, either may be returned.
Below program illustrate max() method of BigInteger class.
/* *Program Demonstrate max() method of BigInteger */import java.math.*;  public class GFG {     public static void main(String[] args) {                 // Create 2 BigInteger objects      BigInteger biginteger=new BigInteger("8976543245");      BigInteger val=new BigInteger("9248040402");              // Call max() method to find greater value      // between two BigIntegers.      BigInteger biggerInteger = biginteger.max(val);        String result = "Bigger Integer between "+biginteger+" and "                      +val+ " is " +biggerInteger;        // Prints the result       System.out.println(result);        }}
Bigger Integer between 8976543245 and 9248040402 is 9248040402

BigInteger min() method: The min() method of the BigInteger returns the BigInteger whose value is the lesser between current BigInteger and BigInteger passed as parameter to method. If both the values are equal, either may be returned.There is a similar method compareTo() available on BigInteger class . The min() method different than compareTo() in a way that in the compareTo() method we have to interpret the result while on the min() method, the smallest BigInteger will be returned.Syntax:public BigInteger min(BigInteger val)Parameters: The method accepts one parameter val which refers to the value with which the minimum is to be computed.Return Value: The method returns the BigInteger whose value is the lesser of this and val. If the values are equal, either may be returned.Below program illustrate min() method of BigInteger class./* *Program Demonstrate min() method of BigInteger */import java.math.*;  public class GFG {     public static void main(String[] args) {      // Create 2 BigInteger objects      BigInteger biginteger=new BigInteger("5782539631");      BigInteger val=new BigInteger("3592633823");              // Call min() method to find lesser value      // between two BigIntegers.      BigInteger biggerInteger = biginteger.min(val);        String result = "lesser Integer between "+biginteger+" and "                      +val+ " is " +biggerInteger;        // Prints the result       System.out.println(result);        }}Output:lesser Integer between 5782539631 and 3592633823 is 3592633823

There is a similar method compareTo() available on BigInteger class . The min() method different than compareTo() in a way that in the compareTo() method we have to interpret the result while on the min() method, the smallest BigInteger will be returned.
Syntax:
public BigInteger min(BigInteger val)
Parameters: The method accepts one parameter val which refers to the value with which the minimum is to be computed.
Return Value: The method returns the BigInteger whose value is the lesser of this and val. If the values are equal, either may be returned.
Below program illustrate min() method of BigInteger class.
/* *Program Demonstrate min() method of BigInteger */import java.math.*;  public class GFG {     public static void main(String[] args) {      // Create 2 BigInteger objects      BigInteger biginteger=new BigInteger("5782539631");      BigInteger val=new BigInteger("3592633823");              // Call min() method to find lesser value      // between two BigIntegers.      BigInteger biggerInteger = biginteger.min(val);        String result = "lesser Integer between "+biginteger+" and "                      +val+ " is " +biggerInteger;        // Prints the result       System.out.println(result);        }}
lesser Integer between 5782539631 and 3592633823 is 3592633823

Reference:
https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#max(java.math.BigInteger)
https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#min(java.math.BigInteger)
Java-BigInteger
Java-Functions
Java-lang package
java-math
Java-math-package
Java
Java-BigInteger
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Constructors in Java
Stream In Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Java Programming Examples
Internal Working of HashMap in Java
Checked vs Unchecked Exceptions in Java
Strings in Java
StringBuilder Class in Java with Examples