This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Doubt in Paramater Passing (Primitives to Wrappers) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in Paramater Passing (Primitives to Wrappers)" Watch "Doubt in Paramater Passing (Primitives to Wrappers)" New topic
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: 38039
    
  22
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: 38039
    
  22
You're welcome
 
Don't get me started about those stupid light bulbs.
 
subject: Doubt in Paramater Passing (Primitives to Wrappers)
 
Similar Threads
Integer VS int
unexplainable result
Why it is false?
Integer == Integer?
Please explain the answers...