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
Well, If you have Protected variables/Method in class pack1class. and you are accessing it in subclass of pack1subclass (Please use Extends pack1class against it). Then, You can not access a Protected variable on Reference of the Super Class. So, pack1class pc=new pack1class(); System.out.println("protected method"+pc.prot); is Wrong!!
You should do it only through Inheritance. like,replacing above code piece with this System.out.println("protected method"+prot);