| Author |
Doubt in Paramater Passing (Primitives to Wrappers)
|
Suresh Rajadurai
Ranch Hand
Joined: Feb 22, 2007
Posts: 58
|
|
Hi Folks,
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.
Much appreciated.
Regards
Suresh.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32604
|
|
|
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.
|
 |
Suresh Rajadurai
Ranch Hand
Joined: Feb 22, 2007
Posts: 58
|
|
|
Thank you.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32604
|
|
You're welcome
|
 |
 |
|
|
subject: Doubt in Paramater Passing (Primitives to Wrappers)
|
|
|