Home Java Integer Methods min() Java Integer min() Method Definition In Java, Integer.min(a, b) returns the smaller of the two int values. If both are equal, it returns that value.
It matches Math.min(int, int), and it exists so you can write Integer::min as a method reference when combining numbers in a stream.
A static method of java.lang.Integer (since Java 8): call it on the class, as Integer.min(...). Integer lives in java.lang, so no import is needed. Integer.min() was added in Java 8.
Syntax Pass the two numbers to compare.
int a = Integer.min(3, 9); // 3
int b = Integer.min(-5, -2); // -5
int c = Integer.min(7, 7); // 7 Call What it does Integer.min(a, b)The smaller of a and b. Math.min(a, b)Same result for int; also has other numeric versions. stream.reduce(Integer::min)Finds the smallest value in a stream of numbers. Integer.min(limit, value)Caps a value so it never goes above limit.
Use min for upper limits and max for lower limits. Combine them to keep a value inside a range.
Library declarations // java.lang.Integer
public static int min(int a, int b) Parameters Parameter Description a (int)The first number. b (int)The second number.
Return Value Return Value Description intThe smaller of a and b.
Important
This method was added in Java 8. It never overflows and never throws. It gives the same result as Math.min(a, b) for int values. A common beginner mix-up is the direction: to put an upper limit on a value use min, and to put a lower limit on it use max. For the smallest element of a collection, Collections.min() is shorter.
Compare with Similar Methods Related methods:
Method How it differs max(a, b)The larger of two values instead. sum(a, b)Another Java 8 method meant for reduce. signum(i)Tells you only whether a number is negative, zero, or positive. Collections.min()The smallest element of a whole collection.
Java Examples 1. Smaller of two 2. Lowest price in an array 3. Cap and clamp Example 1 — Pick the Smaller Number Call min on pairs of numbers, including negative values and the extremes of int.
public class IntegerMinDemo {
public static void main(String[] args) {
System.out.println("min(3, 9): " + Integer.min(3, 9));
System.out.println("min(9, 3): " + Integer.min(9, 3));
System.out.println("min(-5, -2): " + Integer.min(-5, -2));
System.out.println("min(7, 7): " + Integer.min(7, 7));
System.out.println("min(MIN, MAX): " + Integer.min(Integer.MIN_VALUE, Integer.MAX_VALUE));
}
} min(3, 9): 3
min(9, 3): 3
min(-5, -2): -5
min(7, 7): 7
min(MIN, MAX): -2147483648 How It Works Order does not matter. min(3, 9) and min(9, 3) both return 3.Negative numbers. -5 is smaller than -2, because it is further from zero.Equal inputs. min(7, 7) returns 7, so a tie is not a problem.The extremes. Comparing the smallest and largest possible int returns the smaller one, Integer.MIN_VALUE.Example 2 — Find the Lowest Price with Integer::min Use Integer::min as a method reference to reduce an array of prices to the cheapest one.
import java.util.Arrays;
public class IntegerMinReduce {
public static void main(String[] args) {
int[] prices = {450, 399, 520, 405, 399};
int cheapest = Arrays.stream(prices).reduce(Integer.MAX_VALUE, Integer::min);
System.out.println("cheapest price: " + cheapest);
int manual = Integer.MAX_VALUE;
for (int price : prices) {
manual = Integer.min(manual, price);
}
System.out.println("with a loop: " + manual);
}
} cheapest price: 399
with a loop: 399 How It Works A safe starting point. Integer.MAX_VALUE is bigger than any price, so the first real price always replaces it.reduce combines pairs. The stream feeds the current cheapest price and the next price to Integer::min.The same idea in a loop. The loop keeps the smaller of the current best and each price.Same answer. Both end with 399, even though that price appears twice.Example 3 — Cap a Discount and Clamp a Percentage Use min to set an upper limit, then combine it with max to keep a value inside a range.
public class IntegerMinCap {
static int cappedDiscount(int requested) {
return Integer.min(requested, 50);
}
static int clamp(int value, int low, int high) {
return Integer.max(low, Integer.min(value, high));
}
public static void main(String[] args) {
System.out.println("discount 20%: " + cappedDiscount(20));
System.out.println("discount 80%: " + cappedDiscount(80));
System.out.println("clamp(150): " + clamp(150, 0, 100));
System.out.println("clamp(-20): " + clamp(-20, 0, 100));
System.out.println("clamp(55): " + clamp(55, 0, 100));
}
} discount 20%: 20
discount 80%: 50
clamp(150): 100
clamp(-20): 0
clamp(55): 55 How It Works Upper limit with min. A requested 80% discount is cut down to 50, while 20% is already below the cap and stays as it is.Clamp in two steps. min(value, high) caps the top, then max(low, ...) raises the bottom.Too big. 150 is capped to 100 by the inner min.Too small and just right. -20 is lifted to 0 by the outer max, and 55 passes through both unchanged.Frequently Asked Questions What does Integer.min do in Java? It returns the smaller of two int values. Integer.min(3, 9) returns 3.
What is the difference between Integer.min and Math.min? For two ints they give the same result. Integer.min was added in Java 8 mainly so Integer::min can be used as a method reference.
How do I find the smallest number in an int array? Use Arrays.stream(array).min().getAsInt(), or reduce(Integer.MAX_VALUE, Integer::min), or a simple loop.
How do I limit a value to a range? Combine both methods: Integer.max(low, Integer.min(value, high)). The inner min sets the upper limit and the outer max sets the lower limit.
Which Java version added Integer.min? Java 8. In older versions use Math.min(a, b).
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
15 people found this page helpful
Helpful Share Copy link Suggestion