What is Math.MAX() in JAVA?

The MAX() function returns the largest value from the two numbers sent as a parameter.

Figure 1 shows a visual representation of the MAX() function.

Figure 1: Visual representation of MAX() function

Syntax

type max(type num-1, type num-2)

Parameter

The MAX() function takes the two numbers as a parameter.

The numbers can be int, double, float, or long.

Return value

The MAX() function returns the largest value from the two numbers sent as a parameter.

Code

class JAVA {
public static void main( String args[] ) {
// two positive numbers
System.out.println("Maximum between 9 and 8:");
System.out.println(Math.max(9,8));
//one positive and the other negative
System.out.println("Maximum between -9 and 8:");
System.out.println(Math.max(-9,8));
// both negative numbers
System.out.println("Maximum between -9 and -10:");
System.out.println(Math.max(-9,-10));
// both double numbers
System.out.println("Maximum between 12.234 and 1.2345:");
System.out.println(Math.max(12.234,1.2345));
// one int and the other double
System.out.println("Maximum between 12.234 and 1:");
System.out.println(Math.max(12.234,1));
}
}

Free Resources