Skip to content
Related Articles

Related Articles

Improve Article

BigInteger max() and min() Methods in Java

  • Last Updated : 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
    
  • 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
    

Reference:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :