Hi, I am trying to understand the following piece of code.
public static void main(String[] args) { Integer i1 = 1000; Integer i2 = 1000; if(i1 != i2) System.out.println("different objects"); if(i1.equals(i2)) System.out.println("meaningfully equal"); Integer i3 = 10; Integer i4 = 10; if(i3 == i4) System.out.println("same object"); if(i3.equals(i4)) System.out.println("meaningfully equal"); }
This method executes all println instructions. That is, i1! = I2 is true, but i3 == i4. At first glance this seems strange to me, they should be different as links. I can understand that if I pass the same byte value (from -128 to 127) to i3 and i4, they will always be equal as links, but any other value will give them as different.
I canβt explain this, can you point me to some documentation or provide useful information?
thanks
java memory integer
Victor blaga
source share