Doubt in Paramater Passing (Primitives to Wrappers)
Joined: Feb 22, 2007
I have a problem in the following code:
When I compiled this code and ran this, the output was
false, true, false.
But I expected to be false false false. Since when the primitives are wrapped, it become different objects. Can anybody throw some light on it PLEASE.
Joined: Oct 13, 2005
It is probably to do with caching; I think, when you start instantiating Integer objects, the JVM creates a cache of small (-128 <= i < 128) values. When you "box" the 12 into an Integer, the JVM reuses the Integer(12) in the cache. So you have the same Integer(22) twice, so == returns true.