3

Possible Duplicate:
Wrapper class and == operator

Hi when I am comparing Integer with == I have some problem so can you explain me why second test is success too ?

@Test
public void integerTest() {
    Integer prvni = 127;
    Integer druhy = 127;
    Integer treti = 128;
    Integer ctvrty = 128;

    assertTrue(prvni == druhy);
    assertTrue(treti != ctvrty);

}
Community
  • 1
  • 1
hudi
  • 15,555
  • 47
  • 142
  • 246

3 Answers3

11

When using == to compare Objects, you're actually comparing the references. I.e., the reason both assertions are true is because the prvni and druhy refer to the same object while treti and ctvrty does not.

This is because the JVM caches Integer objects in the range -128 to 127, and reuses cached objects when autoboxing the values.

Unless you switch to int instead, you could go through prvni.intValue() or use prvni.equals(...) instead.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • If you change the 128 to a number in that range (e.g. 125) the code works as expected. Why doesn't java cache the numbers on runtime? – leifg Sep 05 '11 at 14:54
  • The range is wrong-- it's -128 to 127. – Platinum Azure Sep 05 '11 at 14:54
  • 1
    @leifg: Java uses an array as the backing store for the `Integer` cache. Arrays cannot be resized at runtime, nor can they be used for sparse structures (i.e., to cache values far apart with no cached objects in between). A `Hashtable` or similar could be used, but those are only allowed to have `Object` keys, which means you'd have to use `Integer` objects (rather than `int` primitives) as the keys, which just brings up the whole problem again! So the only option is to use an array with a sensible range. – Platinum Azure Sep 05 '11 at 15:00
1

Since Java 1.5, some of the wrapper classes have introduced a cache. For Integer, any number between -128 and 127 inclusive fell in the cache. Other values needed to be wrapped in a new Integer every time.

The == operator compares references. Since the cached Integer values for 127 are in fact the very same object, == returns true. For the 128 Integer objects, they are two different objects and do not have the same reference equality.

There are two more reliable ways you can compare for equality:

if (treti.equals(ctvrty)) { /* do something */ }

or:

if (treti.compareTo(ctvrty) == 0) { /* do something */ }

The latter comparison takes advantage of the fact that Integer implements the Comparable interface and thus defines a compareTo method which returns a negative value if the first object is "less than" the second, a positive value if the first object is "greater than" the second, and zero if the objects compare equal.

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
-2

The autoboxing feature creates a new instance for every object.

try this:

int treti = 128;
int ctvrty = 128;
leifg
  • 8,668
  • 13
  • 53
  • 79
  • -1: Doesn't explain why the 127 case works and doesn't completely answer the question. Look into the wrapper class caching. – Platinum Azure Sep 05 '11 at 14:56