Home >>Java Math Methods >Java Math.max() method

Java Math.max() method

Java Math.max() method

The math.max() method in Java Math is an inbuilt Java method which is used to return Maximum or Largest value from the two arguments given. The arguments in int, float, double, and long are taken up.

Syntax

public static int max(int a, int b)  
public static double max(double a, double b)  
public static long max(long a, long b)  
public static float max(float a, float b)  

Parameters

a: This is used for first value

b: This parameter is used for second value

Returns

This method is used to returns maximum of two numbers.

  1. This method will return positive argument if we have positive and negative value as argument.
  2. If we have both negative values as argument, the result is returned number with the lower magnitude.
  3. If the arguments are not number(NaN), then will be returned by this method.

Java Math.max() Method Example 1


public class MyClass   
{  
    public static void main(String args[])  
    {  
        int a = 15;  
        int b = 35;  
        System.out.println(Math.max(a, b));  
    }  
}  

Output:
35

Java Math.max() Method Example 2


public class MyClass   
{  
    public static void main(String args[])  
    {  
        double a = 10.06;  
        double b = -12.12;  
        System.out.println(Math.max(a, b));  
    }  
}  

Output:
10.06