Compare two Integer: why is == true? [duplicate]
Asked Answered
P

3

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);

}
Personal answered 5/9, 2011 at 14:46 Comment(1)
Also: #5117632Absolute
T
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.

Tijuana answered 5/9, 2011 at 14:47 Comment(3)
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?Schleswig
The range is wrong-- it's -128 to 127.Hersey
@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.Hersey
H
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.

Hersey answered 5/9, 2011 at 14:54 Comment(0)
S
-2

The autoboxing feature creates a new instance for every object.

try this:

int treti = 128;
int ctvrty = 128;
Schleswig answered 5/9, 2011 at 14:51 Comment(1)
-1: Doesn't explain why the 127 case works and doesn't completely answer the question. Look into the wrapper class caching.Hersey

© 2022 - 2024 — McMap. All rights reserved.