CKTIS / Java
Math.max — the greater of two numbers
Math.max returns the greater of two numbers. Learn its numeric overloads and its behavior for NaN and signed zero.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
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)Math.max(a, b) returns the greater of two arguments. It is static: call it through Math, without creating an object. It has four overloads: int, long, float, and double; each returns its corresponding type.
Equal arguments produce that same value. For float and double, either argument being NaN makes the result NaN. Comparing -0.0 with +0.0 returns positive zero, although comparison operators treat those zeros as equal.
The example demonstrates integer calculations and both floating-point special cases.
Examples
public class Main {
public static void main(String[] args) {
System.out.println(Math.max(-4, 7));
System.out.println(Math.max(12L, 5L));
System.out.println(Math.max(Float.NaN, 3.0f));
System.out.println(Math.max(-0.0, +0.0));
}
}Result
7 12 NaN 0.0
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.