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".
Integer.compare(3, 7); // negative (-1) Integer.compare(7, 7); // 0 Integer.compare(9, 7); // positive (1)
Try it
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.
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
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::comparefitsComparator<Integer>and can be passed straight tosort.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 useCollections.reverseOrder(). Swapping is clearer and safer than negating a comparator's result, because a custom comparator may returnInteger.MIN_VALUE, which cannot be negated. - Unsigned order.
compareUnsigned(-1, 1)is positive, since-1is4294967295when read as unsigned. - Other types.
Long.compare,Double.compare,Character.compare,Boolean.compareandShort.comparefollow the same pattern.Double.comparealso ordersNaNand-0.0consistently, unlike<and==. - Equality.
compare(x, y) == 0andx == yagree for primitives. ForIntegerobjects, useequalsorcompareTo, not==. - Null safety. To sort a list that may contain
null, wrap the comparator withComparator.nullsFirst(...)ornullsLast(...).