The Math.max() method returns the greater of two numeric values across int, long, float, and double primitives.
Overloaded Boundary Selection: Enforces strict IEEE 754 rules for NaN propagation and signed zeros (+0.0 > -0.0). For the smaller of two numbers, see Math.min(). For magnitude without sign, explore Math.abs(). For directional rounding, see Math.ceil(), Math.floor(), and Math.round().
Defined in java.lang.Math for all four numeric primitive types:
// Four overloaded signatures of Math.max() in 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)
| Overload Signature | Return Type | Type Promotion & Operational Rule |
|---|---|---|
Math.max(int a, int b) |
int |
Also handles byte, short, and char via widening promotion. |
Math.max(long a, long b) |
long |
Selected when at least one integer argument is a 64-bit long. |
Math.max(float a, float b) |
float |
Propagates Float.NaN and orders +0.0f above -0.0f. |
Math.max(double a, double b) |
double |
Selected when either argument is a double; propagates Double.NaN. |
Comparing integers, floating-point numbers, and negative values (where numbers closer to zero are greater):
System.out.println(Math.max(23, 12)); // 23
System.out.println(Math.max(23.4, 12.56)); // 23.4
System.out.println(Math.max(-23, -12)); // -12
System.out.println(Math.max(10L, 500L)); // 500Terminal Output:
23
23.4
-12
500
Unlike the relational operator >, Math.max() propagates NaN from either position and distinguishes +0.0 from -0.0:
System.out.println(Math.max(5.0, Double.NaN)); // NaN
System.out.println(Math.max(Double.NaN, 5.0)); // NaN
double z = Math.max(-0.0, +0.0);
System.out.println(1.0 / z); // Infinity (+0.0)Terminal Output:
NaN
NaN
Infinity
Chaining nested calls or passing Math::max as a method reference to Java Streams:
import java.util.Arrays;
int max3 = Math.max(42, Math.max(89, 17));
System.out.println(max3); // 89
int[] arr = {-45, -12, -3, -29};
System.out.println(Arrays.stream(arr).reduce(Integer.MIN_VALUE, Math::max)); // -3Terminal Output:
89
-3
Why Math.max() is not identical to a naive ternary expression for floating-point values:
| Input Pair (a, b) | Math.max(a, b) | (a > b) ? a : b | Behavioral Difference |
|---|---|---|---|
(Double.NaN, 5.0) |
NaN | 5.0 (Swallows NaN!) |
Math.max never hides corrupted NaN states. |
(+0.0, -0.0) |
+0.0 | -0.0 (Since +0.0 > -0.0 is false) |
Math.max strictly enforces +0.0 > -0.0. |
(20, 10) |
20 | 20 |
Identical for all integer primitives; JIT compiles to branchless CPU instructions. |
Combining Math.max() with Math.min() to constrain a variable within a valid range:
public class RangeClamper {
public static int clamp(int val, int min, int max) {
return Math.min(max, Math.max(min, val));
}
public static void main(String[] args) {
System.out.println(clamp(-15, 0, 100)); // 0
System.out.println(clamp(45, 0, 100)); // 45
System.out.println(clamp(180, 0, 100)); // 100
}
}Terminal Output:
0
45
100
Eliminating negative values in neural networks and financial surplus calculations:
public class ActivationAndBilling {
public static double relu(double x) {
return Math.max(0.0, x);
}
public static void main(String[] args) {
System.out.println(relu(-4.5)); // 0.0
System.out.println(relu(12.8)); // 12.8
}
}Terminal Output:
0.0
12.8
Never initialize a running maximum accumulator to 0 when scanning signed data; always initialize to Integer.MIN_VALUE (or the first element):
int[] temps = {-18, -5, -12};
int wrong = 0, right = Integer.MIN_VALUE;
for (int t : temps) {
wrong = Math.max(wrong, t);
right = Math.max(right, t);
}
System.out.println("Init 0: " + wrong + " | Init MIN: " + right);Terminal Output:
Init 0: 0 | Init MIN: -5
Unlike Integer.MIN_VALUE (-2147483648), Double.MIN_VALUE is a tiny positive number (4.9E-324)! For doubles, initialize accumulators to Double.NEGATIVE_INFINITY or -Double.MAX_VALUE.
Calling Math.max(intVal, doubleVal) promotes the int to double and returns a double, requiring an explicit cast if assigning back to an integer.
You can compile and run this complete verification class locally in any Java IDE or environment:
public class MathMaxFullDemo {
public static void main(String[] args) {
// 1. Verify int and double overloads
if (Math.max(10, 20) == 20 && Math.max(-5.5, -2.2) == -2.2) {
System.out.println("Check 1 Passed: Primitive overloads verified.");
}
// 2. Verify NaN propagation
if (Double.isNaN(Math.max(100.0, Double.NaN))) {
System.out.println("Check 2 Passed: NaN propagation verified.");
}
// 3. Verify signed zero ordering
if (Double.compare(Math.max(-0.0, +0.0), +0.0) == 0) {
System.out.println("Check 3 Passed: +0.0 ordered above -0.0.");
}
System.out.println("All Math.max() verification checks passed successfully!");
}
}Terminal Output:
Check 1 Passed: Primitive overloads verified.
Check 2 Passed: NaN propagation verified.
Check 3 Passed: +0.0 ordered above -0.0.
All Math.max() verification checks passed successfully!
No, Math.max() only accepts two parameters. For three values, nest calls: Math.max(a, Math.max(b, c)). For many values, use IntStream.of(...).max() or Collections.max().
Java automatically widens byte, short, and char values to int, invoking Math.max(int, int).
Yes, starting in Java 21, Math.clamp(value, min, max) is built into the standard library. In Java 8 through 20, use Math.min(max, Math.max(min, value)).
Returns Double.POSITIVE_INFINITY.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.