aspose file tools*
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 Spring in Action this week in the Spring 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: 39416
    
  28
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: 39416
    
  28
You're welcome
 
Consider Paul's rocket mass heater.
 
subject: Doubt in Paramater Passing (Primitives to Wrappers)