Comportamiento de las clases de envoltura extraña con == y! = [Duplicado]

Posible duplicado:
Weird Java Boxing

Recently while I was reading about wrapper classes I came through this strange case:

Integer i1 = 1000;
Integer i2 = 1000;

if(i1 != i2) System.out.println("different objects");

if(i1 == i2) System.out.println("same object");

Que imprime:

different objects

y

Integer i1 = 10;
Integer i2 = 10;

if(i1 != i2) System.out.println("different objects");

if(i1 == i2) System.out.println("same object");

Que imprime:

same object

Is there any reasonable explanation for this case?

Muchas Gracias

preguntado el 09 de enero de 11 a las 06:01

Esta es la razón .equals() existe. -

This seems like it breaks all sorts of invariants. Are you sure you read this correctly? Can you reproduce it? I'd just like some more confirmation on this. -

Are you sure? I just tried this out, and my program output is "different objects" which is what I would expect? Did you make a typo? What jvm are you using and what system? -

OK, now your edit makes sense. I undeleted my answer. -

Did you actually compile and run this code? It does not do what you claim. -

3 Respuestas

La razón por la cual == returns true for the second case is because the primitive values boxed by the wrappers are sufficiently small to be interned to the same value at runtime. Therefore they're equal.

In the first case, Java's integer cache is not large enough to contain the number 1000, so you end up creating two distinct wrapper objects, comparing which by reference returns false.

The use of said cache can be found in the Integer#valueOf(int) método (donde IntegerCache.high defaults to 127):

public static Integer valueOf(int i) {
    if(i >= -128 && i <= IntegerCache.high)
        return IntegerCache.cache[i + 128];
    else
        return new Integer(i);
}

As Amber says, if you use .equals() then both cases will invariably return true because it unboxes them where necessary, then compares their primitive values.

Respondido el 09 de enero de 11 a las 10:01

So it is the runtime library having a cache for the byte value range, and not the JVM intern'ing it. - Thorbjørn Ravn Andersen

+1 for the nice explanation with "Java's integer cache is not large enough to contain the number 1000, so you end up creating two distinct wrapper objects" etc - namalfernandolk

So, when both i1 and i2 have the same value between -128 and 127, i1==i2 returned true, else false. - namalfernandolk

Integer i1 = 1000;

compiler understands it as an int that is why i1 == i2 // return true

but while i1 y i2 are big == test return false

Integer i1 = 10000000;
Integer i2 = 10000000;

if(i1 != i2) System.out.println("different objects"); // true

if(i1 == i2) System.out.println("same object"); // false

aviso:

Integer i1 = 100;
Integer i2 = 100;

System.out.println(i1 == i2); // true

i1 = 1000000;
i2 = 1000000;

System.out.println(i1 == i2); // false

do not test equality with == for Objects. it just compares their reference. .equal() check whether two objects are same or not.

Integer i1 = 1000000;
Integer i2 = 1000000;

i1 == i2 // false
i1.equals(i2) // true

Respondido 04 Feb 12, 10:02

"compiler understands it as an integer that is why i1 == i2 // return true" This doesn't make any sense as if you tried for example 700 it gives you different objects aren't this small value - Feras Odeh

I mean int a simple data type not object - user467871

I just tried this, and all it prints for me is

different objects

as expected, since you are creating two different wrapper objects, even though they happen to contain the same value.

As Amber implies in the above comment,

if(i1.equals(i2)) System.out.println("same value");

does indeed print

same value

Respondido el 09 de enero de 11 a las 09:01

Downvote? For what? I compiled and ran the code, this is what I saw. :-/ - metadaddy

I just tried this, and all it prints for me is this is not true. try to give the value from the range -127 to 127 it will print as same object. - Gunaseelan

Different Java versions? Two years on, I have no idea what I was running back then! - metadaddy

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.