Java Math.max()

max() accepts two numbers as arguments, and returns the greater of the two values.

Following is the syntax of max() method.

int result = max(int a, int b)
long result = max(long a, long b)
float result = max(float a, float b)
double result = max(double a, double b)

The datatype of return value is based on the datatype of arguments.

We shall learn about max() method in detail with the help of examples.

Example 1 – Math.max(int, int)

In the following example, we use will pass two integers as arguments to max() method and find the largest of the two integer numbers.

Java Program

public class MathExample {
	public static void main(String[] args) {
		int a = 10;
		int b = 5; 
		int result = Math.max(a, b);
		System.out.println(result);
	}
}

Output

10
ADVERTISEMENT

Example 2 – Math.max(long, long)

In the following example, we use will pass two long numbers as arguments to max() method and find the largest of the two numbers.

Java Program

public class MathExample {
	public static void main(String[] args) {
		long a = 1099859955445L;
		long b = 58978542L; 
		long result = Math.max(a, b);
		System.out.println(result);
	}
}

Output

1099859955445

Example 3 – Math.max(float, float)

In the following example, we use will pass two floating point numbers as arguments to max() method and find the largest of the two float numbers.

Java Program

public class MathExample {
	public static void main(String[] args) {
		float a = 10.9985F;
		float b = 5.89F; 
		float result = Math.max(a, b);
		System.out.println(result);
	}
}

Output

10.9985

Example 4 – Math.max(double, double)

In the following example, we use will pass two numbers (of type double) as arguments to max() method and find the largest of the two double numbers.

Java Program

public class MathExample {
	public static void main(String[] args) {
		double a = 10.9985;
		double b = 585566.89963333; 
		double result = Math.max(a, b);
		System.out.println(result);
	}
}

Output

585566.89963333

Example 5 – Math.max(x, y) – x or y as NaN

In the following example, we use will pass NaN for one of the two arguments. If any of the arguments to max() method is NaN, then the return value should be NaN.

Java Program

public class MathExample {
	public static void main(String[] args) {
		double a = 10.9985;
		double b = Double.NaN; 
		double result = Math.max(a, b);
		System.out.println(result);
	}
}

Output

NaN

Example 6 – Math.max(float, int)

If you provide arguments of different datatype, then the lower datatype is promoted to higher datatype, and largest value with the respective datatype is returned.

Java Program

public class MathExample {
	public static void main(String[] args) {
		int a = 5;
		float b = 10.2F; 
		float result = Math.max(a, b);
		System.out.println(result);
	}
}

Output

10.2

In this example, among int and float, float is higher datatype and hence, a is promoted to float. max(int, float) becomes max(float, float).

Conclusion

In this Java Tutorial, we learned about Java Math.max() function, with example programs.