BigInteger max() and min() Methods in Java

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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    /*
     *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);
         
       }
    }

    chevron_right

    
    

    Output:

    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.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    /*
     *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);
         
       }
    }

    chevron_right

    
    

    Output:

    lesser Integer between 5782539631 and 3592633823 is 3592633823
    

Reference:



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.