Java Integer compare() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

In Java, Integer.compare(x, y) compares two int values. It returns a negative number when x is less than y, zero when they are equal, and a positive number when x is greater. In practice the answer is always -1, 0, or 1.

It is the right tool inside a Comparator. It never overflows, and it works on primitive values, so no boxing is needed.

A static method of java.lang.Integer (since Java 1.7): call it on the class, as Integer.compare(...). Integer lives in java.lang, so no import is needed.

Syntax

Pass the two numbers to compare.

Java
int a = Integer.compare(3, 7);    // -1 (3 is less than 7)
int b = Integer.compare(7, 3);    // 1
int c = Integer.compare(5, 5);    // 0
CallWhat it does
Integer.compare(x, y)Negative, zero, or positive, for less, equal, or greater.
Integer.compare(y, x)Swapped arguments give descending order in a comparator.
(a, b) -> Integer.compare(a, b)A lambda comparator for ascending order.
x.compareTo(y)The same comparison on Integer objects.

Test the result with < 0, == 0, or > 0. Do not rely on the exact value being -1 or 1, even though that is what it returns today.

Library declarations
Java
// java.lang.Integer
public static int compare(int x, int y)

Parameters

ParameterDescription
x (int)The first number.
y (int)The second number, compared against x.

Return Value

Return ValueDescription
negative intx is less than y.
0x and y are equal.
positive intx is greater than y.

Compare with Similar Methods

Related comparison methods:

MethodHow it differs
compareTo(other)The instance version, called on an Integer object.
compareUnsigned(x, y)Treats both numbers as unsigned 32-bit values.
max(x, y)Returns the larger value instead of a comparison result.
String.compareTo()Compares text, and returns the difference of the first differing characters.

Java Examples

Example 1 — Compare Pairs of Numbers

Compare four pairs and print a plain-English answer next to the result.

Java
public class IntegerCompareDemo {
    static String describe(int result) {
        if (result < 0) {
            return "less";
        } else if (result > 0) {
            return "greater";
        }
        return "equal";
    }

    public static void main(String[] args) {
        int[][] pairs = {{3, 7}, {7, 3}, {5, 5}, {-10, 4}};
        for (int[] pair : pairs) {
            int result = Integer.compare(pair[0], pair[1]);
            System.out.println(pair[0] + " vs " + pair[1] + ": " + result + " (" + describe(result) + ")");
        }
    }
}

How It Works

  1. Three outcomes. The helper turns the number into a word, because only the sign of the result matters.
  2. 3 vs 7. The first number is smaller, so the result is negative (-1) and the word is "less".
  3. 7 vs 3. Swapping the arguments flips the sign, giving 1 and "greater".
  4. Equal and negative inputs. 5 vs 5 gives 0, and -10 vs 4 is "less" because negative numbers are smaller than positive ones.

Example 2 — Sort Scores Ascending and Descending

Use Integer.compare in a lambda comparator, and swap the arguments to reverse the order.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class IntegerCompareSort {
    public static void main(String[] args) {
        List<Integer> scores = new ArrayList<>(Arrays.asList(70, 95, 82, 60, 95));

        scores.sort((a, b) -> Integer.compare(a, b));
        System.out.println("ascending:  " + scores);

        scores.sort((a, b) -> Integer.compare(b, a));
        System.out.println("descending: " + scores);
    }
}

How It Works

  1. A comparator is a function of two values. List.sort calls it again and again to decide which element comes first.
  2. Normal order. compare(a, b) is negative when a is smaller, so smaller scores move to the front.
  3. Reversed order. Calling compare(b, a) swaps the roles, so the biggest score comes first.
  4. Duplicates stay. The two 95 values compare as 0, so both stay in the list next to each other.

Example 3 — Spot the Overflow Bug in a - b

Compare the old subtraction trick with Integer.compare on a pair of extreme values.

Java
public class IntegerCompareOverflow {
    static int bad(int a, int b) {
        return a - b;
    }

    public static void main(String[] args) {
        int[][] pairs = {{3, 10}, {Integer.MAX_VALUE, -1}, {Integer.MIN_VALUE, 1}};
        for (int[] pair : pairs) {
            System.out.println(pair[0] + " vs " + pair[1]
                    + ": a - b = " + bad(pair[0], pair[1])
                    + ", compare = " + Integer.compare(pair[0], pair[1]));
        }
    }
}

How It Works

  1. Small values behave. 3 minus 10 is -7, which has the right sign, while compare says -1.
  2. MAX_VALUE minus -1 overflows. The true answer 2147483648 does not fit in an int, so it wraps to -2147483648, and the code would wrongly say that MAX_VALUE is smaller.
  3. MIN_VALUE minus 1 overflows too. It wraps around to the largest positive number, so the sign is wrong again.
  4. compare is always right. It never subtracts; it only looks at which value is bigger.

Frequently Asked Questions

A negative number if the first int is smaller, 0 if they are equal, and a positive number if the first is larger. In practice it returns -1, 0, or 1.
compare is static and takes two primitive ints. compareTo is called on an Integer object and takes one other Integer. They give the same result.
The subtraction can overflow and give the wrong sign for large or negative values. Integer.compare(a, b) is safe.
Use a comparator that swaps the arguments: list.sort((a, b) -> Integer.compare(b, a)).
Java 7. On older versions write (a < b) ? -1 : (a == b ? 0 : 1) yourself.

Next: compareTo()

Compare an Integer object with another Integer.

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

9 people found this page helpful