Java Integer max() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 8+

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.

Java
int a = Integer.max(3, 9);     // 9
int b = Integer.max(-5, -2);    // -2
int c = Integer.max(7, 7);      // 7
CallWhat 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
// java.lang.Integer
public static int max(int a, int b)

Parameters

ParameterDescription
a (int)The first number.
b (int)The second number.

Return Value

Return ValueDescription
intThe greater of a and b.

Compare with Similar Methods

Related methods:

MethodHow it differs
min(a, b)The smaller of two values instead.
sum(a, b)Another Java 8 method meant for reduce.
compare(a, b)Tells you which is larger but does not return the value.
Collections.max()The largest element of a whole collection.

Java Examples

Example 1 — Pick the Larger Number

Call max on pairs of numbers, including negative values and the extremes of int.

Java
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));
    }
}

How It Works

  1. Order does not matter. max(3, 9) and max(9, 3) both return 9.
  2. Negative numbers. -2 is larger than -5, because it is closer to zero.
  3. Equal inputs. When both numbers match, the result is that same number.
  4. 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.

Java
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);
    }
}

How It Works

  1. A safe starting point. Integer.MIN_VALUE is smaller than any score, so the first real score always replaces it.
  2. reduce combines pairs. The stream feeds the running maximum and the next score to Integer::max, one pair at a time.
  3. The same idea in a loop. The loop keeps the larger of the current best and each score.
  4. 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.

Java
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));
    }
}

How It Works

  1. A floor of zero. max(0, ...) returns zero whenever the real answer would be negative.
  2. Normal sales. Selling 4 of 10 leaves 6, which is above the floor, so it is returned unchanged.
  3. Overselling. Selling 15 would give -5, but the floor turns it into 0.
  4. Any minimum works. max(1, page) makes sure a page number is never below 1.

Frequently Asked Questions

It returns the larger of two int values. Integer.max(3, 9) returns 9.
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.
Use Arrays.stream(array).max().getAsInt(), or reduce(Integer.MIN_VALUE, Integer::max), or a simple loop that keeps the larger value.
Not in one call. Nest the calls, as in Integer.max(a, Integer.max(b, c)), or use reduce for many values.
Java 8. In older versions use Math.max(a, b).

Next: min()

Return the smaller of two int values.

min() 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.

14 people found this page helpful