Home Java Integer Methods max() Java Integer max() Method Definition In Java, Integer.max(a, b) returns the larger of the two int values. If both are equal, it returns that value.
It does the same job as Math.max(int, int), but it was added to Integer so it can be used as a method reference, such as Integer::max, to combine numbers in a stream.
A static method of java.lang.Integer (since Java 8): call it on the class, as Integer.max(...). Integer lives in java.lang, so no import is needed. Integer.max() was added in Java 8.
Syntax Pass the two numbers to compare.
int a = Integer.max(3, 9); // 9
int b = Integer.max(-5, -2); // -2
int c = Integer.max(7, 7); // 7 Call What it does Integer.max(a, b)The larger of a and b. Math.max(a, b)Same result for int; also has long, float, and double versions. stream.reduce(Integer::max)Finds the largest value in a stream of numbers. Integer.max(0, value)Keeps a value from dropping below 0.
Integer.max only takes two int values. To find the largest of many numbers, call it repeatedly or use reduce.
Library declarations // java.lang.Integer
public static int max(int a, int b) Parameters Parameter Description a (int)The first number. b (int)The second number.
Return Value Return Value Description intThe greater of a and b.
Important
This method was added in Java 8. It never overflows and never throws, because it only compares. It is exactly Math.max(a, b) for int values, so choose whichever reads better; the real benefit of Integer.max is the method reference Integer::max. If you need the largest element of a collection, Collections.max() is shorter.
Compare with Similar Methods Related methods:
Java Examples 1. Larger of two 2. Highest value in an array 3. Never below a floor Example 1 — Pick the Larger Number Call max on pairs of numbers, including negative values and the extremes of int.
public class IntegerMaxDemo {
public static void main(String[] args) {
System.out.println("max(3, 9): " + Integer.max(3, 9));
System.out.println("max(9, 3): " + Integer.max(9, 3));
System.out.println("max(-5, -2): " + Integer.max(-5, -2));
System.out.println("max(7, 7): " + Integer.max(7, 7));
System.out.println("max(MIN, MAX): " + Integer.max(Integer.MIN_VALUE, Integer.MAX_VALUE));
}
} max(3, 9): 9
max(9, 3): 9
max(-5, -2): -2
max(7, 7): 7
max(MIN, MAX): 2147483647 How It Works Order does not matter. max(3, 9) and max(9, 3) both return 9.Negative numbers. -2 is larger than -5, because it is closer to zero.Equal inputs. When both numbers match, the result is that same number.The extremes. Comparing the smallest and largest possible int returns Integer.MAX_VALUE.Example 2 — Find the Highest Score with Integer::max Use Integer::max as a method reference to reduce an array of scores to its highest value.
import java.util.Arrays;
public class IntegerMaxReduce {
public static void main(String[] args) {
int[] scores = {72, 88, 95, 61, 79};
int highest = Arrays.stream(scores).reduce(Integer.MIN_VALUE, Integer::max);
System.out.println("highest score: " + highest);
int manual = Integer.MIN_VALUE;
for (int score : scores) {
manual = Integer.max(manual, score);
}
System.out.println("with a loop: " + manual);
}
} highest score: 95
with a loop: 95 How It Works A safe starting point. Integer.MIN_VALUE is smaller than any score, so the first real score always replaces it.reduce combines pairs. The stream feeds the running maximum and the next score to Integer::max, one pair at a time.The same idea in a loop. The loop keeps the larger of the current best and each score.Same answer. Both ways end with 95, the highest of the five scores.Example 3 — Keep a Value from Going Below Zero Use max(0, value) to stop stock levels and balances from turning negative.
public class IntegerMaxFloor {
static int afterSale(int stock, int sold) {
return Integer.max(0, stock - sold);
}
public static void main(String[] args) {
System.out.println("stock 10, sold 4: " + afterSale(10, 4));
System.out.println("stock 10, sold 10: " + afterSale(10, 10));
System.out.println("stock 10, sold 15: " + afterSale(10, 15));
int page = -3;
System.out.println("page number: " + Integer.max(1, page));
}
} stock 10, sold 4: 6
stock 10, sold 10: 0
stock 10, sold 15: 0
page number: 1 How It Works A floor of zero. max(0, ...) returns zero whenever the real answer would be negative.Normal sales. Selling 4 of 10 leaves 6, which is above the floor, so it is returned unchanged.Overselling. Selling 15 would give -5, but the floor turns it into 0.Any minimum works. max(1, page) makes sure a page number is never below 1.Frequently Asked Questions What does Integer.max do in Java? It returns the larger of two int values. Integer.max(3, 9) returns 9.
What is the difference between Integer.max and Math.max? For two ints they give the same result. Integer.max was added in Java 8 mainly so Integer::max can be used as a method reference.
How do I find the largest number in an int array? Use Arrays.stream(array).max().getAsInt(), or reduce(Integer.MIN_VALUE, Integer::max), or a simple loop that keeps the larger value.
Can Integer.max compare more than two numbers? Not in one call. Nest the calls, as in Integer.max(a, Integer.max(b, c)), or use reduce for many values.
Which Java version added Integer.max? Java 8. In older versions use Math.max(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.
14 people found this page helpful
Helpful Share Copy link Suggestion