The Math.min() method returns the smaller of two numeric values across int, long, float, and double primitives.
Safe Upper-Bound Capping: Enforces IEEE 754 rules for NaN propagation and signed zeros (-0.0 < +0.0). For the greater of two numbers, see Math.max(). For unsigned magnitude, explore Math.abs(). For directional rounding, see Math.floor(), Math.ceil(), and Math.round().
Defined in java.lang.Math for all four numeric primitive types:
// Four overloaded signatures of Math.min() in java.lang.Math
public static int min(int a, int b)
public static long min(long a, long b)
public static float min(float a, float b)
public static double min(double a, double b)
| Overload Signature | Return Type | Type Promotion & Operational Rule |
|---|---|---|
Math.min(int a, int b) |
int |
Handles byte, short, and char via automatic widening promotion. |
Math.min(long a, long b) |
long |
Selected when at least one integer argument is a 64-bit long. |
Math.min(float a, float b) |
float |
Propagates Float.NaN and orders -0.0f below +0.0f. |
Math.min(double a, double b) |
double |
Selected when either argument is a double; propagates Double.NaN. |
Comparing positive and negative numbers (where values further to the left on the number line are smaller):
System.out.println(Math.min(23, 12)); // 12
System.out.println(Math.min(23.4, 12.56)); // 12.56
System.out.println(Math.min(-23, -12)); // -23 (further left on number line)
System.out.println(Math.min(10L, 500L)); // 10Terminal Output:
12
12.56
-23
10
Unlike the relational operator <, Math.min() propagates NaN from either argument and considers -0.0 strictly smaller than +0.0:
System.out.println(Math.min(5.0, Double.NaN)); // NaN
System.out.println(Math.min(Double.NaN, 5.0)); // NaN
double z = Math.min(+0.0, -0.0);
System.out.println(1.0 / z); // -Infinity (proves z is -0.0, not +0.0)Terminal Output:
NaN
NaN
-Infinity
Chaining nested calls or passing Math::min as a method reference to Java Streams:
import java.util.Arrays;
int min3 = Math.min(42, Math.min(89, 17));
System.out.println(min3); // 17
int[] prices = {145, 92, 188, 75, 129};
System.out.println(Arrays.stream(prices).reduce(Integer.MAX_VALUE, Math::min)); // 75Terminal Output:
17
75
How Math.min() differs from a manual ternary comparison on floating-point edge cases:
| Input Pair (a, b) | Math.min(a, b) | (a < b) ? a : b | Behavioral Difference |
|---|---|---|---|
(Double.NaN, 5.0) |
NaN | 5.0 (Swallows NaN!) |
Math.min preserves NaN error signaling. |
(-0.0, +0.0) |
-0.0 | +0.0 (Since -0.0 < +0.0 is false) |
Math.min strictly orders -0.0 < +0.0. |
(10, 20) |
10 | 10 |
Identical for integer primitives; JIT emits branchless CMOV instructions. |
Capping slice upper bounds with Math.min() prevents IndexOutOfBoundsException on the final page of a list:
public class SafeSliceBounds {
public static int safeEndIndex(int startOffset, int pageSize, int totalItems) {
// Prevent IndexOutOfBoundsException on the final partial page
return Math.min(startOffset + pageSize, totalItems);
}
public static void main(String[] args) {
System.out.println(safeEndIndex(0, 10, 25)); // 10 (Page 1: 0..10)
System.out.println(safeEndIndex(20, 10, 25)); // 25 (Page 3: 20..25, capped at 25!)
}
}Terminal Output:
10
25
Enforcing a maximum monetary ceiling on percentage-based discounts or transaction fees:
public class DiscountCapping {
public static double applyCappedDiscount(double orderTotal, double rate, double maxCap) {
return Math.min(orderTotal * rate, maxCap);
}
public static void main(String[] args) {
System.out.println(applyCappedDiscount(200.0, 0.15, 50.0)); // 30.0 (below cap)
System.out.println(applyCappedDiscount(600.0, 0.15, 50.0)); // 50.0 (capped at 50.0)
}
}Terminal Output:
30.0
50.0
Never initialize a running minimum accumulator to 0 when scanning positive data; always initialize to Integer.MAX_VALUE (or Double.POSITIVE_INFINITY):
int[] latencies = {42, 18, 29};
int wrong = 0, right = Integer.MAX_VALUE;
for (int ms : latencies) {
wrong = Math.min(wrong, ms);
right = Math.min(right, ms);
}
System.out.println("Init 0: " + wrong + " | Init MAX: " + right);Terminal Output:
Init 0: 0 | Init MAX: 18
To clamp val into [low, high], use Math.max(low, Math.min(high, val)). Reversing the bounds (passing high to max) locks the output to a constant.
If offset is near Integer.MAX_VALUE, offset + size overflows to a negative number before Math.min() runs. Use long arithmetic for large file offsets.
You can compile and run this complete verification class locally in any Java IDE or environment:
public class MathMinFullDemo {
public static void main(String[] args) {
// 1. Verify int and double overloads
if (Math.min(10, 20) == 10 && Math.min(-5.5, -2.2) == -5.5) {
System.out.println("Check 1 Passed: Primitive overloads verified.");
}
// 2. Verify NaN propagation
if (Double.isNaN(Math.min(100.0, Double.NaN))) {
System.out.println("Check 2 Passed: NaN propagation verified.");
}
// 3. Verify signed zero ordering (-0.0 < +0.0)
if (Double.compare(Math.min(+0.0, -0.0), -0.0) == 0) {
System.out.println("Check 3 Passed: -0.0 ordered below +0.0.");
}
System.out.println("All Math.min() verification checks passed successfully!");
}
}Terminal Output:
Check 1 Passed: Primitive overloads verified.
Check 2 Passed: NaN propagation verified.
Check 3 Passed: -0.0 ordered below +0.0.
All Math.min() verification checks passed successfully!
Nest two calls: Math.min(a, Math.min(b, c)).
Returns -20, because -20 is mathematically less than -10.
Math.min() always returns NaN if either argument is NaN.
No. Integer.min(a, b) simply delegates to Math.min(a, b) and was added in Java 8 as a convenient method reference for streams.
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.