Yes, Line 3 Integer i=2 also creates a new Object. But there is a difference between Integer i=2 //Line 1 and Integer i=new Integer(2); //Line 2
How? Here is an Example to Illustrate it:-
Integer i1 = new Integer(2); Integer i2 = new Integer(2); if(i1 != i2) System.out.println("different objects");
This would always Print "different objects" since new Object is created everytime you use new Keyword.
But if you do it like this, Integer i1 = 2; Integer i2 = 2; if(i1 != i2) System.out.println("different objects");
Both i1 and i2 refers to same Object !.So it wont Print "different objects".
However it is subject to some conditions, two instances of the wrapper objects will refer to same Object when their primitive values are the same and each of these is : Boolean Byte Character from \u0000 to \u007f (7f is 127 in decimal) Short and Integer from -128 to 127