What does this print, and why?
Integer a = 127, b = 127;
Integer c = 128, d = 128;
System.out.println(a == b);
System.out.println(c == d);Show the explanation
It prints true then (normally) false.
Autoboxing turns Integer a = 127 into Integer.valueOf(127). valueOf returns cached objects for −128 to 127, so a and b are the same object and == (which compares references) is true. 128 is outside the cache, so c and d are two different objects and == is false. (The upper limit can be raised with a JVM flag, which is why it is "normally" false.)
The lesson: compare wrapper objects with equals (or unbox them):
System.out.println(c.equals(d)); // true
System.out.println(c.intValue() == d); // true: d is unboxedTwo more boxing traps: unboxing a null Integer throws NullPointerException, and list.remove(1) on a List<Integer> removes the element at index 1, not the value 1.
Original UniCircle question. Last reviewed 22 Oct 2026.
