• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Paramater Passing (Primitives to Wrappers)

 
Suresh Rajadurai
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic