Integer i = 5; Integer j = 5; if (i == j) System.out.println("true");
^ Prints true
Integer i = new Integer(5); Integer j = new Integer(5); if (i == j) System.out.println("true");
^ Does not print true.
I understand that in the second case, the == is comparing the references. But why not in the first case? It almost seems like the compiler is treating it as two 'ints' in the first case, since it knows in advance from the declaration. But shouldn't it be autoboxed as an Integer? Thanks