18

Double has Double.compare for comparing two double primitives. Why doesn't Integer have one?

I understand it's some trivial amount of code to write, but asking out of curiosity.

Edit: I realize both Integer and Double have compareTo. But using compareTo requires boxing the int primitive in an Integer object, which has a pretty high cost. Also, inta > intb is not the same as compare(inta, intb), as the latter returns +1, 0, or -1, while the former is true/false ....

rxin
  • 1,786
  • 16
  • 25
  • 1
    [It does](http://download.oracle.com/javase/6/docs/api/java/lang/Integer.html#compareTo\(java.lang.Integer\)). – jiggy Aug 02 '11 at 18:56
  • 1
    It does not have a `compare(int a, int b)` method, however you could use: `new Integer(a).compareTo(b)` – ty1824 Aug 02 '11 at 18:56
  • @ty1824 that requires boxing the primitive into an object though ... – rxin Aug 02 '11 at 19:09
  • 4
    I understand. Also, from your edits, what do you need from this answer? Do you need the +1, 0, or -1? Would you be fine with just a +N, 0, or -N? If so, you could simply subtract the second integer from the former one. `int result = first - second;` And if all you want is the +1, 0, or -1, you take the magnitude of result. `result /= Math.abs(result)` – ty1824 Aug 02 '11 at 19:34
  • 1
    Boxing does not have "a pretty high cost". – Steve Kuo Aug 02 '11 at 20:29

5 Answers5

19

It was a oversight that Java 7 will resolve

http://download.oracle.com/javase/7/docs/api/java/lang/Integer.html#compare%28int,%20int%29

public static int compare(int x, int y)

Compares two int values numerically. The value returned is identical to what would be returned by:

Integer.valueOf(x).compareTo(Integer.valueOf(y))

Parameters: x - the first int to compare y - the second int to compare Returns: the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > y Since: 1.7

Preston
  • 3,273
  • 4
  • 26
  • 35
  • 1
    I wouldn't say an oversight, more a deliberate omission based on the fact it's trivial for ints as oppose to floating point numbers. They've probably just had a rethink for 7 - but yes it is included in it! – Michael Berry Jan 08 '12 at 22:51
6

The compare in Double has the same effect as:

new Double(d1).compareTo(new Double(d2))

Which means it takes NaN, +0 and -0 into account (quoting the doc for compareTo()):

  • Double.NaN is considered by this method to be equal to itself and greater than all other double values (including Double.POSITIVE_INFINITY).
  • 0.0d is considered by this method to be greater than -0.0d.

Since Integer does not have NaN, and both +0 and -0 will be regarded as just 0, such a method is not really needed for functionality.

zw324
  • 26,764
  • 16
  • 85
  • 118
4

Comparing ints in this way is trivial, comparing doubles is actually much more complicated than it might first seem. You have to deal with things such as error values, and less obviously cases such as NaN.

See this question for details.

Either way, as of Java 7, you'll have such a method for ints as well!

Community
  • 1
  • 1
Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • It might be trivial, but honestly I always forget which should be 1 or which should be -1. It's not that it takes me long to remember, but the idea of taking logic (which this does qualify as) and putting it in a billion places just because it's trivial is, in my opinion, a poor decision. – corsiKa Jan 23 '12 at 21:34
2

Floating point values cannot necessarily be binarily compared, because of imprecision in the floating point representation, therefore, a compare() operator is required to compare two floating point values, essentially making sure that the difference between them is not greater than a (small) error value. Integers can be binarily compared, so can use the equality operator.

Paul Sonier
  • 38,903
  • 3
  • 77
  • 117
  • +1 but remember that equality operations must be done using an `int`, not an `Integer` – ty1824 Aug 02 '11 at 18:59
  • 3
    You *can* compare floating-point values with a simple `<` operator, you just have to know what you are doing. Additionally, the `Double.compare` method does not use this *compare with a small difference* "equality", just because it doesn't need to. – Roland Illig Aug 02 '11 at 19:01
0

It's probably because the compare operation is relatively simple (but still many people get it wrong). And compared to double, it really is.

I too would like to have this method built-in, which makes other people write simpler code. But as long as Oracle doesn't see it as a problem, we have to rely on Google's Guava or similar libraries to provide the missing bits.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121