Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrapper Pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrapper Pool" Watch "Wrapper Pool" New topic

Wrapper Pool

Sac Anand

Joined: Apr 21, 2006
Posts: 19
In K&B 5.0 (Page 234), it is written that

In order to save memory, two instances of the
following wrapper objects will always be == when their primitive values are the same:
- Boolean
- Byte
- Character from \u0000 to \u007f (7f is 127 in decimal)
- Short and Integer from -128 to 127

My understanding is that the implementation for the above is same as the String pool is maintained. Please validate.

My doubt is that apart from the above mentioned Wrapper Classes, Long also works in the same way but it is not covered in the above statement from K&B.
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132
It does not work in the same way as the String pool, but sort of:

new Long(7) == 7, is true
String str = "7";
str == "7", is true but
new String(7) == "7", is false
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Why does the autoboxing conversion sometimes return the same reference?

Also, The Java Language Specification does not explicitly guarantee this behavior for long values within the range of a byte.
Sac Anand

Joined: Apr 21, 2006
Posts: 19
Thanks Petrus and Wise.
Got your point.
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
According to JLS, Long is not guaranteed to behave the same way, as the rule above.

SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
I agree. Here's the link:
subject: Wrapper Pool
It's not a secret anymore!