Java caches the integer objects in the range -128 to 127
Integer i = 126;
Integer i2 = 126;
Integer i3 = 128;
Integer i4 = 128;
System.out.println(i == i2); // true, reference pointing to same literal
System.out.println(i3 == i4); // false, reference pointing to different objects
Integer i = new Integer(126);
Integer i2 = new Integer(126);
System.out.println(i == i2); // false
Integer i = 126;
Integer i2 = 126;
Integer i3 = 128;
Integer i4 = 128;
System.out.println(i == i2); // true, reference pointing to same literal
System.out.println(i3 == i4); // false, reference pointing to different objects
Integer i = new Integer(126);
Integer i2 = new Integer(126);
System.out.println(i == i2); // false
Комментариев нет:
Отправить комментарий