I have following code:
Integer first = new Integer(4);
Integer second = new Integer(4);
System.out.println(first == second);
Integer third = 4;
System.out.println(first == third);
System.out.println(second == third);
As 4 falls between -128 to 127, I expect that Integer object wrapping 4 is cached once its created first time, and then its returned for other boxing statements, and so '==' check must return true. But its always false for above three cases.
Why is it so?