Java Math min() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Math

Definition

In Java, Math.min(a, b) returns the smaller of its two arguments. Math.min(3, 8) is 3.

Besides finding the lowest value, min is the standard way to set an upper limit: Math.min(100, level) never goes above 100.

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.min(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass two numbers. Mixed types are widened to the larger type first.

Java
int low = Math.min(72, 95);                 // 72
double cheaper = Math.min(4.99, 3.49);      // 3.49
int level = Math.min(100, battery + 30);    // never above 100
int end = Math.min(start + 10, total);      // last index of a page
CallWhat it does
Math.min(4, 9)int overload: 4.
Math.min(-5, 3L)Mixed: -5 widens to long, result -5L.
Math.min(100, x)Upper limit: x, but never more than 100.
Math.min(a, Math.min(b, c))Smallest of three values.

min and max are mirror images. Together, Math.max(lo, Math.min(hi, v)) keeps v inside a range; Java 21 adds Math.clamp for that.

Library declarations
Java
// 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)

Parameters

ParameterDescription
aThe first value.
bThe second value.

Return Value

Return ValueDescription
Same type as the (widened) argumentsThe smaller of a and b. If they are equal, that value.

Compare with Similar Methods

Related ways to pick or limit values:

MethodHow it differs
max(a, b)Returns the larger value; used for lower limits.
clamp(v, lo, hi)Both limits at once. Java 21+.
a < b ? a : bSame for ints; differs for NaN and -0.0.
Collections.min(list)Smallest element of a whole collection.

Java Examples

Example 1 — Find the Cheapest Price

Compare shop prices and keep the lowest one.

Java
public class MathMinCheapest {
    public static void main(String[] args) {
        double[] prices = {4.99, 3.49, 5.25, 3.75};
        double cheapest = prices[0];
        for (int i = 1; i < prices.length; i++) {
            cheapest = Math.min(cheapest, prices[i]);
        }
        System.out.println("Cheapest: " + cheapest);
        System.out.println("Smaller of 7 and -2: " + Math.min(7, -2));
    }
}

How It Works

  1. Seed with a real price. Starting from prices[0] avoids a magic start value and works for any array with at least one element.
  2. Keep the lower one. Each step compares the current cheapest with the next price: 4.99 drops to 3.49, and neither 5.25 nor 3.75 is lower.
  3. Negatives count as smaller. min compares values, not sizes, so -2 beats 7.

Example 2 — Cap a Value and Split Items into Pages

Use min so a battery level never passes 100 percent and the last page never runs past the end of the list.

Java
public class MathMinCap {
    public static void main(String[] args) {
        int battery = 85;
        battery = Math.min(100, battery + 30);
        System.out.println("Battery: " + battery + "%");

        int total = 23;
        int pageSize = 10;
        for (int start = 0; start < total; start += pageSize) {
            int end = Math.min(start + pageSize, total);
            System.out.println("Page shows items " + (start + 1) + " to " + end);
        }
    }
}

How It Works

  1. Upper limit. 85 + 30 would be 115, but Math.min(100, 115) keeps the battery at 100.
  2. Full pages. For the first two pages start + pageSize (10, then 20) is still below 23, so it is the smaller value and becomes end.
  3. Short last page. On the third page 30 is past the end, so min picks total and the page stops at item 23.

Example 3 — Mixed Types, Negative Zero, and NaN

Check the result type of mixed arguments and the double special cases.

Java
public class MathMinSpecial {
    public static void main(String[] args) {
        System.out.println("int vs long:   " + Math.min(-5, 3L));
        System.out.println("float vs int:  " + Math.min(1.5f, 2));
        System.out.println("-0.0 vs 0.0:   " + Math.min(-0.0, 0.0));
        System.out.println("NaN vs 1.0:    " + Math.min(Double.NaN, 1.0));
    }
}

How It Works

  1. Result type. -5 widens to long and 2 widens to float, so the calls return a long and a float respectively.
  2. Negative zero is smaller. min returns -0.0, which println shows with its minus sign.
  3. NaN wins again. As with max, a NaN argument makes the result NaN.

Frequently Asked Questions

The smaller of its two arguments, in the same type as the arguments. Math.min(3, 8) returns 3.
Use Math.min(limit, value). For example Math.min(100, level) never returns more than 100.
Combine both methods: Math.max(lo, Math.min(hi, value)). On Java 21 or newer you can call Math.clamp(value, lo, hi) instead.
It is -0.0. Math.min treats negative zero as smaller than positive zero, even though -0.0 == 0.0 is true.
No, it takes exactly two. Nest calls, such as Math.min(a, Math.min(b, c)), or loop over an array.

Next: multiplyExact()

Multiply two ints or longs and throw ArithmeticException on overflow.

multiplyExact() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful