posible duplicado:
Integer wrapper objects share the same instances only within the value 127?
How != and == operators work on Integers in Java?resultado inesperado cuando Ints comparan
Me trataron de comparar dos enteros con los siguientes casos y tiene resultados inesperados
cuando hice el siguiente wing, @@@ fue impreso.
class C { static Integer a = 127; static Integer b = 127; public static void main(String args[]){ if(a==b){ System.out.println("@@@"); } } }
cuando hice lo siguiente, @@@ no se imprimió.
class C { static Integer a = 145; static Integer b = 145; public static void main(String args[]){ if(a==b){ System.out.println("@@@"); } } }
Puede alguien decirme lo que podría ser la razón.
Si alguien puede formatear it..I no soy capaz de ... – Rookie
posible duplicado de objetos envolventes [Integer comparten los mismos casos sólo dentro de el valor 127?] (http://stackoverflow.com/questions/5117132/integer-wrapper-objects-share-the-same-instances-only-within-the-value-127) y [New Integer vs valueOf] (http://stackoverflow.com/questions/2974561) y [Comportamiento incoherente en java ==] (http://stackoverflow.com/questions/1148805) y [los objetos contenedores de enteros comparten las mismas instancias solo dentro del valor 127?] (http://stackoverflow.com/questions/5117132) y ... –
Más detalles si está interesado http://vanillajava.blogspot.co.uk/2012/01/surprising-results-of-autoboxing.html –