Open In App

StrictMath max() Method in Java With Examples

Last Updated : 25 Jul, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The java.lang.StrictMath.max() method returns the larger of two values. There are four variations of this method with different types of parameters passed.
All of them are discussed below :

  1. The max(double num1, double num2)is the inbuilt method which is used to get the maximum of two given double values arguments. It returns the same value when num1 and num2 have the same value. It returns NaN when either value is NaN, then the result is . It takes up negative zero to be strictly smaller than positive zero. It returns positive zero when one argument is positive zero and the other negative zero.

    Syntax :

    public static double max(double num1, double num2)

    Parameters : The method accepts a two parameter :

    • num1 of double type representing the parameter
    • num2 of double type representing another parameter

    Return Value : The method returns the greater of num1 and num2.

    Examples :

    Input: 
    num1 = 8
    nm2 = 19
    Output: 19.0
    

    Below programs illustrate the Java.lang.StrictMath.max() method.
    Program 1:




    // Java praogram to illustrate the
    // Java.lang.StrictMath.max()
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            double num1 = 117, num2 = 711;
      
            double max_Value = StrictMath.max(num1, num2);
            System.out.println("max of the two num is " + max_Value);
        }
    }

    
    

    Output:

    max of the two num is 711.0
    

    Program 2:




    // Java praogram to illustrate the
    // Java.lang.StrictMath.max()
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            double num1 = -87, num2 = -11;
      
            double max_Value = StrictMath.max(num1, num2);
            System.out.println("max of the two num is " + max_Value);
        }
    }

    
    

    Output:

    max of the two num is -11.0
    

    Example of Error Condition :




    // Java praogram to illustrate the
    // error condition in
    // Java.lang.StrictMath.max() Method
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            double num1 = 51, num2 = 71, num3 = 3, num4 = -93,
                   num5 = -93;
            double a = 0.0;
      
            num1 = a / 0.0;
            double max_Value = StrictMath.max(num1, a);
            System.out.println("max of the two num is " + max_Value);
        }
    }

    
    

    Output:

    max of the two num is NaN
    
  2. The max(float num1, float num2) is the inbuilt method which is used to get the maximum of two given float values arguments. It returns the same value when num1 and num2 have the same value. It returns NaN when either value is NaN. It takes up negative zero to be strictly smaller than positive zero. It returns positive zero when one argument is positive zero and the other negative zero.

    Syntax :

    public static float max(float num1, float num2)

    Parameters : The method accepts a two parameter :

    • num1 of float type representing the parameter
    • num2 of float type representing another parameter

    Return Value : The method returns the greater of num1 and num2.

    Examples :

    Input: 
    num1 = 87
    nm2 = 59
    Output: 87.0
    

    Below programs illustrate the Java.lang.StrictMath.max() method.
    Program 1:




    // Java praogram to illustrate the
    // Java.lang.StrictMath.max()
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            float num1 = 75, num2 = 81, num3 = -62, num4 = -62, num5 = -92;
      
            float max_Value = StrictMath.max(num1, num2);
            System.out.println("max of the two num is " + max_Value);
      
            float max_Value1 = StrictMath.max(num3, num4);
            System.out.println("max of the two num is " + max_Value1);
      
            float max_Value2 = StrictMath.max(num4, num5);
            System.out.println("max of the two num is " + max_Value2);
        }
    }

    
    

    Output:

    max of the two num is 81.0
    max of the two num is -62.0
    max of the two num is -62.0
    
  3. The max(int num1, int num2)is the inbuilt method which is used to get the maximum of two given int values arguments. It returns the same value when num1 and num2 have the same value. It returns NaN when either value is NaN. It returns positive zero when one argument is positive zero and the other negative zero. In short, it returns the argument that is closer to the value of Integer.MAX_VALUE.
    Syntax :

    public static int max(int num1, int num2)

    Parameters : The method accepts a two parameter :

    • num1 of int type representing the parameter
    • num2 of int type representing another parameter

    Return Value : The method returns the greater of num1 and num2.

    Examples :

    Input: 
    num1 = 13
    nm2 = 19
    Output: 19.0
    

    Below programs illustrate the Java.lang.StrictMath.max() method.
    Program 1:




    // Java praogram to illustrate the
    // Java.lang.StrictMath.max()
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            int num1 = 51, num2 = 72, num3 = -31, num4 = -31, num5 = -89;
      
            int max_Value = StrictMath.max(num1, num2);
            System.out.println("max of the two num is " + max_Value);
      
            int max_Value1 = StrictMath.max(num3, num4);
            System.out.println("max of the two num is " + max_Value1);
      
            int max_Value2 = StrictMath.max(num4, num5);
            System.out.println("max of the two num is " + max_Value2);
        }
    }

    
    

    Output:

    max of the two num is 72
    max of the two num is -31
    max of the two num is -31
    
  4. The max(long num1, long num2) is the inbuilt method which is used to get the maximum of two given long values arguments. It returns the same value when num1 and num2 have the same value. It returns NaN when either value is NaN. In short it returns the argument that is closer to the value of Long.MAX_VALUE.
    Syntax :

    public static long max(long num1, long num2)

    Parameters : The method accepts a two parameter :

    • num1 of long type representing the parameter
    • num2 of long type representing another parameter

    Return Value : The method returns the greater of num1 and num2.

    Examples :

    Input: 
    num1 = 78772728
    nm2 =  1617177
    Output: 78772728.0
    

    Below programs illustrate the Java.lang.StrictMath.max() method.
    Program 1:




    // Java praogram to illustrate the
    // Java.lang.StrictMath.max()
    import java.lang.*;
      
    public class Geeks {
      
        public static void main(String[] args)
        {
      
            long num1 = 87287, num2 = 787822, num3 = -3271, num4 = -3271,
                 num5 = -459;
      
            long max_Value = StrictMath.max(num1, num2);
            System.out.println("max of the two num is " + max_Value);
      
            long max_Value1 = StrictMath.max(num3, num4);
            System.out.println("max of the two num is " + max_Value1);
      
            long max_Value2 = StrictMath.max(num4, num5);
            System.out.println("max of the two num is " + max_Value2);
        }
    }

    
    

    Output:

    max of the two num is 787822
    max of the two num is -3271
    max of the two num is -459
    


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads