In Java, Math.max(a, b) returns the larger of its two arguments. Math.max(3, 8) is 8.
Typical uses: tracking the highest score in a loop, and setting a lower limit, as in Math.max(0, stock - sold), so that a value never drops below zero.
A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.max(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass two numbers. If their types differ, Java widens the smaller type first.
Java
int best = Math.max(72, 95); // 95
double top = Math.max(1.5, -2.0); // 1.5
int left = Math.max(0, stock - sold); // never below 0
int big3 = Math.max(a, Math.max(b, c)); // largest of three
Call
What it does
Math.max(4, 9)
int overload: 9.
Math.max(4, 9.0)
Mixed: 4 widens to double, result 9.0.
Math.max(0, x)
Lower limit: x, but never less than 0.
Math.max(a, Math.max(b, c))
Largest of three values.
max takes exactly two arguments. For arrays, loop and keep a running maximum, or use Arrays.stream(arr).max() (Java 8+), which returns an optional result.
Library declarations
Java
// java.lang.Math
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)
Parameters
Parameter
Description
a
The first value.
b
The second value.
Return Value
Return Value
Description
Same type as the (widened) arguments
The larger of a and b. If they are equal, that value.
Compare with Similar Methods
Methods and operators for picking or limiting values:
Keep a running maximum while walking through an array, and nest calls for three values.
Java
public class MathMaxHighScore {
public static void main(String[] args) {
int[] scores = {72, 95, 64, 88};
int best = Integer.MIN_VALUE;
for (int s : scores) {
best = Math.max(best, s);
}
System.out.println("Best score: " + best);
System.out.println("Max of three: " + Math.max(10, Math.max(40, 25)));
}
}
Output
Best score: 95
Max of three: 40
How It Works
Start below everything.best begins at Integer.MIN_VALUE, so the first score always replaces it.
Running maximum. Each pass keeps whichever is larger, the old best or the current score: 72, then 95, which 64 and 88 cannot beat.
Nesting. The inner call picks 40 from 40 and 25, and the outer call compares that with 10.
Example 2 — Use max() as a Lower Limit
Stock and prices must not go negative. Math.max(0, ...) turns a negative result into zero.
Java
public class MathMaxNoNegative {
public static void main(String[] args) {
int stock = 5;
int ordered = 8;
int left = Math.max(0, stock - ordered);
System.out.println("Items left: " + left);
double price = 19.99;
double coupon = 25.0;
System.out.println("To pay: " + Math.max(0.0, price - coupon));
}
}
Output
Items left: 0
To pay: 0.0
How It Works
A negative difference. 5 - 8 is -3, which makes no sense as a stock level.
Zero wins.Math.max(0, -3) picks 0, so the shop shows zero items instead of minus three.
Same idea for money. The coupon is worth more than the price, so the difference is negative and the customer pays 0.0 (a double, because both arguments are doubles).
Example 3 — Mixed Types, Negative Zero, and NaN
See how argument types decide the result type, and how max handles the unusual double values.
Java
public class MathMaxSpecial {
public static void main(String[] args) {
System.out.println("int vs double: " + Math.max(3, 9.0));
System.out.println("long: " + Math.max(5L, 2_000_000_000_000L));
System.out.println("-0.0 vs 0.0: " + Math.max(-0.0, 0.0));
System.out.println("NaN vs 100: " + Math.max(Double.NaN, 100.0));
}
}
Output
int vs double: 9.0
long: 2000000000000
-0.0 vs 0.0: 0.0
NaN vs 100: NaN
How It Works
Widening. One argument is a double, so 3 becomes 3.0 and the double overload returns 9.0 with a decimal point.
Signed zeros are ordered. The two zeros are equal for ==, but max deliberately treats 0.0 as the larger one.
NaN is contagious. A NaN argument means "unknown", so the maximum is unknown too, even next to 100.0.
Frequently Asked Questions
The larger of its two arguments, in the same type as the arguments. Math.max(3, 8) returns the int 8.
Nest the calls: Math.max(a, Math.max(b, c)). For many values, loop over them and keep a running maximum.
Start with the first element or Integer.MIN_VALUE, then update it with best = Math.max(best, x) for every element. With streams you can also use Arrays.stream(arr).max().getAsInt().
NaN. Any comparison involving NaN is unknown, so the result is NaN too.
Yes. The int is widened to double and the double version runs, so Math.max(3, 9.0) returns 9.0.