Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

Java Integer.compare()

Reference for Java Integer.compare(): compare two ints safely, why subtraction breaks comparators, compareTo, compareUnsigned and sorting with it.

Integer.compare(x, y) compares two int values and returns a negative number if x < y, zero if they are equal, and a positive number if x > y. It matches the contract of Comparator.compare and Comparable.compareTo, which makes it the right building block for custom sort orders.

The most common reason to reach for it is to avoid writing a - b in a comparator. Subtraction looks neat but overflows for large operands, producing a wrong sign and a sort that is silently incorrect or even throws "Comparison method violates its general contract".

Java
Integer.compare(3, 7);   // negative (-1)
Integer.compare(7, 7);   // 0
Integer.compare(9, 7);   // positive (1)

Try it

Java compare versus subtraction

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Using it in sort orders

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
static int compare(int x, int y)
static int compareUnsigned(int x, int y)
int compareTo(Integer other)
Name Description
x, y The two values to compare.
other The Integer to compare an instance with (must not be null).

Returns: -1, 0 or 1 in practice. The specification only promises "less than zero", "zero" or "greater than zero" for Comparator, but Integer.compare and compareTo return exactly those three values. Do not depend on that when you write code against the Comparator interface.

Throws: nothing for primitives; compareTo(null) throws NullPointerException.

Usage notes

  • Never write return a - b;. It breaks for extreme values: Integer.MAX_VALUE - (-10) wraps to a negative number, so the larger value sorts first.
  • Method reference. Integer::compare fits Comparator<Integer> and can be passed straight to sort. Comparator.naturalOrder() does the same job.
  • Prefer key extractors. Comparator.comparingInt(Player::score) avoids boxing and is clearer than a handwritten lambda; reverse it with .reversed().
  • Reversing. Swap the arguments (compare(q, p)) or use Collections.reverseOrder(). Swapping is clearer and safer than negating a comparator's result, because a custom comparator may return Integer.MIN_VALUE, which cannot be negated.
  • Unsigned order. compareUnsigned(-1, 1) is positive, since -1 is 4294967295 when read as unsigned.
  • Other types. Long.compare, Double.compare, Character.compare, Boolean.compare and Short.compare follow the same pattern. Double.compare also orders NaN and -0.0 consistently, unlike < and ==.
  • Equality. compare(x, y) == 0 and x == y agree for primitives. For Integer objects, use equals or compareTo, not ==.
  • Null safety. To sort a list that may contain null, wrap the comparator with Comparator.nullsFirst(...) or nullsLast(...).