thanks and regards venu.
At the statement "i1++", "i1" gets unwrapped, incremented and rewrapped and refers to a different object, but "i2" still refers to "i1" (thus they refer to the same object), so the second "i1 == i2" should also return true, but it doesn't.
You have to understand the distinction between reference and object.
i2 = i1 ... means assign the i2 reference to the object that is currently referenced by i1. So, after that operation both the i1 and i2 reference refers to the same object. If you change the i1 reference to something else, it doesn't automatically change the i2 reference.
venu chakravorty wrote:The following piece of code makes me think that: "i1" refers to "new Integer(5)" and i2 refers to the same "new Integer(5)" through "i1", i.e. "i2" refers to "i1" which refers to the "new Integer(5)".
What you're describing is an assignment by reference, but Java doesn't have that. In Java everything is done by value. Variables are assigned by value and parameters are passed by value. Nothing is done by reference. Still there word reference is used in Java but here it means "pointer to an object". you should be careful to make these distinctions,
* Java never assigns variables by reference. Java always assigns variables by value and the value is either a references (a pointer) or a primitive.
* Java never passes parameters by reference. Java always passes parameters by value and the value is either a references (a pointer) or a primitive.
So "by reference" is completely banned from Java. A reference in Java is a pointer.