Hi Rubayat,
When you write:
Integer i1 =10;
Before
Java 5.0 it was compile error to do so. You couldn't have assigned an int to Integer reference. But thanks to Autoboxing, primitive int is automatically boxed to Integer.
The compiler does the job for you: Internally it does:
Integer i1 = Integer(10);
But to save memory when the range of int is withing 127 (max range of byte), comparison of two references that are created with the following way
always equal
Integer i1 = 10;
Integer i2 = 10;
if(i1==i2) {
System.out.println("==");
}
When you create an a wrapper class object using new operator a new object is created an assigned to the wrapper reference. So each time you create an
object using new, new object is created on the heap. So the references will point to different objects regardless of the encapsulating primitive. Consequently your comparison to two references results false even if both refer to the objects encapsulating equal primitive, in your case it is 10.
I hope this helps you.
Regards,
CMBHATT
[ March 07, 2007: Message edited by: Chandra Bhatt ]