+ 4
Help me to understand
Integer i1 = 127; Integer i2 = 127; System.out.println(i1==i2); i1 = 128; i2 = 128; System.out.println(i1==i2); Why answer is: true false
1 Respuesta
+ 5
I've found an answer.
When you compile a number literal in Java and assign it to a Integer (capital I) the compiler emits:
Integer b2 =Integer.valueOf(127)
This line of code is also generated when you use autoboxing.
valueOf is implemented such that certain numbers are "pooled", and it returns the same instance for values smaller than 128.