public class AutoBoxingAndUnBoxing
{
public static void main(String[] args)
{
Integer x = 127;
Integer y = 127;
System.out.println(x == y);//true
Integer a = 128;
Integer b = 128;
System.out.println(a == b);//false
System.out.println(a); // prints 128
}
}
Come mai x==y
è vero e a==b
è falso? Se si basa sul valore (Integer -128 To 127
), allora 'a'
dovrebbe stampare -128
giusto?Confronto di riferimento usando l'operatore ==
L'intervallo di "Integer" è -2^31 a 2^31 - 1. Non è compreso tra -128 e 127; questo è l'intervallo di un 'byte'. –