# Math.max() Method in Java

Math.max() Method in Java use in java to find maximum number to given two number. Math.max Java method is defined in java.lang.Math class . Java Math.max method Returns the greater of two given values.There are several overloaded forms of the Math.max java method as

## Syntax of Math.max()

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

There is max a static method and we can access using class name Math it .

## max() Parameters :

max method takes two arguments arg1 , arg2 . These argument can be int ,long , float and double.

## max() Return Value

Math.max method return greater value of given two arguments .

## Math.max() Examples in Java

``````package com.demo.program;

public class MathMax {
public static void main(String[] args) {
System.out.println(Math.max(10, 20));
System.out.println(Math.max(10.5, 9));
System.out.println(Math.max(100, 50));
}

}``````

Output :

``````20
10.5
100``````

## Special Cases of Java Math.max()

1). If the arguments have the same values, then the result is also the same value.

``````public class MathMax {
public static void main(String[] args) {
System.out.println(Math.max(10, 10));
}

}``````

Output :

``10``

2). If we provide positive and negative value as argument, this method will return positive argument.

``````public class MathMax {
public static void main(String[] args) {
System.out.println(Math.max(2, -5));
}

}``````

Output :

``2``

3). If we provide both negative values as argument, number with the lower magnitude is returned as result.

``````public class MathMax {
public static void main(String[] args) {
System.out.println(Math.max(-2, -5));
}

}``````
``-2``

4). If the arguments are not a number(NaN), this method will return NaN.

``````public class MathMax {
public static void main(String[] args) {
System.out.println(Math.max(Double.NaN, -5));
System.out.println(Math.max(80.5f , Double.NaN));
System.out.println(Math.max(70 , Double.NaN));
}

}``````

Output :

``````NaN
NaN
NaN``````

In this article we have learned about Java math.max method . This is very useful method if you want to compare two numbers . math.max java is static method and can access with class name Math. You can learn more java coding program for interview .