How is Integer x1 = 5;
different than Integer x1 = new Integer(5);
Integer x1 = 5; // created through boxing
Integer x2 = 5;
Integer x3 = new Integer(5); // no boxing
Integer x4 = new Integer(5);
if (x1 == x2) System.out.println("Same object"); //prints
if (x3 == x4) System.out.println("Same object"); //doesn't print
This code displays doesn't display Same object twice, as I had expected. Why?
PS: The rule is: in order to save memory, two instances of the following wrapper objects will always be == when their primitive values are the same: Boolean; Byte; Characterfrom \u to \u007f; Short and Integer from -128 to 127