Read this.
Java uses a pool for Integer
s in the range from -128 to 127.
That means if you create an Integer
with Integer i = 42;
and its value is between -128 and 128, no new object is created but the corresponding one from the pool is returned. That is why c1
is indeed identical to c2
.
(I assume you know that ==
compares references, not values, when applied to objects).