Hey I am trying to understand the following code snippet.
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 runs all of the println instructions. That is i1 != i2 is true, but i3 == i4. At first glance this strikes me as strange, they should be all different as references. I can figure out that if I pass the same byte value (-128 to 127) to i3 and i4 they will be always equal as references, but any other value will yield them as different.
I can't explain this, can you point me to some documentation or give some helpful insights?
Thank you