I did a little more investigation using Java 6. It appears that a Wrapper Object is == to a primitive, if the value of the Wrapper Object is exactly equal to the value of the primitive. Please see the output of a test program that I wrote:
The fact that there is a pool of bytes in the range of 127 to -128 in the JVM doesn't seem to come into play in these comparisons. The type of the Wrapper Object doesn't seem to make any difference either; it is strictly a value based comparison.
S2.equals(7) is false, but (S2 == 7) is true. Short S2 = new Short((short)7); Why?
In S2.equals(7) first argument is type of Short and second becomes Integer after auto boxing, so equals fails as they belongs to different Object hierarchy.
(S2 == 7) is true because == comparison looks into bit pattern and both points to same value 7 hence tru.
This discussion thread is a continuation of a previous thread from day before yesterday. I guess my question is this, "Are the various Wrapper Objects unboxed to primitives of the same type, and then compared to the corresponding literal or primitive on the other side of the == operator without any further conversions of either operand?"