I am not able to understand the result. I know when == used then wrapper will be unwrapped and primitive values will be used in comparision. so line 5 is ok(please clarify if i m wrong) but line 4 is printing false as i know that to save memory Integer(-127 to 128) will retrun true if there primitive values are save. if this is true then why line 4 is printing false.
Line 4 is testing whether two objects refered to by i1 and i2 refer to the same object which is not true so it prints false. And two objects are considered equal only they are created using the short hand new to java 5, as in Integer i = 5;
I think you are confusing with what had been sayed on this forum a few days ago:
The specification states that "Character from \u0000 to \u007f, Boolean, Short and Integer from -128 to 127" are cached, and hence, will be "==", via boxing when their primitives values are the same.
You obtain i1 via boxing and i2 by calling the constructor. So you cannot apply that rule. The references of the two objects are different. And then System.out.println(i1==i2); will printed false. [ August 15, 2007: Message edited by: Collins Mbianda ]
If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.