Home »
        Java »
        Java Reference »
        Java BigInteger Class
    
    Java BigInteger Class | max() Method with Example
    
    
    
            
        BigInteger Class max() method: Here, we are going to learn about the max() method of BigInteger Class with its syntax and example.
        Submitted by Preeti Jain, on May 11, 2020
    
    BigInteger Class max() method
    
        - max() method is available in java.math package.
- max() method is used to get the highest value from both of the compared objects (this BigInteger and BigInteger val).
- max() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- max() method does not throw an exception at the time of returning maximum.
Syntax:
    public BigInteger max(BigInteger val);
    Parameter(s):
    
        - BigInteger val – represents the object to compare with this object for finding maximum.
Return value:
    The return type of this method is BigInteger, it returns the maximum in anyone of the both compared objects.
            
    Example:
// Java program to demonstrate the example 
// of BigInteger max(BigInteger val) method of BigInteger
import java.math.*;
public class MaxOfBI {
    public static void main(String args[]) {
        // Initialize two variables str1 and str2
        String str1 = "100";
        String str2 = "80";
        // Initialize two BigInteger objects  
        BigInteger b_int1 = new BigInteger(str1);
        BigInteger b_int2 = new BigInteger(str2);
        // Display b_int1 and b_int2
        System.out.println("b_int1: " + b_int1);
        System.out.println("b_int2: " + b_int2);
        // returns the BigInteger that holds the maximum
        // value in both of the compared object
        // i.e. 100.max(80) it returns 100
        BigInteger max_val = b_int1.max(b_int2);
        System.out.println("b_int1.max(b_int2): " + max_val);
    }
}
Output
b_int1: 100
b_int2: 80
b_int1.max(b_int2): 100
    
    
  
    Advertisement
    
    
    
  
  
    Advertisement