This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Integer unboxing and == operator question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Integer unboxing and == operator question" Watch "Integer unboxing and == operator question" New topic
Author

Integer unboxing and == operator question

randy pausch
Greenhorn

Joined: Jun 26, 2008
Posts: 6
Page 284 of the K&B book states

In order to save memory, two instances of the
following wrapper objects (created through boxing), will always be == when their
primitive values are the same:

Short and Integer from -128 to 127



Why does the same rule NOT apply to values over 127 ? What is so special about the -128 to 127 (byte) range?

Sample Code:


prints "!="

while



prints "=="
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Just search in the forum for "integer pool" or "integer 127" and you'll get a result like this...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
randy pausch
Greenhorn

Joined: Jun 26, 2008
Posts: 6
Sorry...I shud have searched before asking.

But guessing the right search query would've been a challenge though.

Thanks Ankit.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Payel please start a new topic for your problem by clicking .

Also when you post the code in the new topic, please Use Code Tags...
Payel Bera
Greenhorn

Joined: Jan 28, 2010
Posts: 10
Ankit Garg wrote:Payel please start a new topic for your problem by clicking .

Also when you post the code in the new topic, please Use Code Tags...


Hi ankit,

The topic was a continuation of the same query so does not need to be included as a separate topic/ new topic.
Regarding code tag will keep that in mind in the next post, for sure.
BTW can you/ anyone else provide the explanation, thanks
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Payel, you can edit your message using button and then add code tags to it.

Coming to the question, what part of the output did you not understand??
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820



In order to save memory, two instances of the
following wrapper objects (created through boxing), will always be == when their
primitive values are the same:

Short and Integer from -128 to 127


"created through boxing" is the key.

Integer.valueOf() returns an int . This int is autoboxed into an Integer, so the "Created through boxing" requirement is met with this one.

new Integer(10) : no boxing here. rule does not apply.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066



This your code....

The below syntax is only creates a new wrapper object on the Integer Literal Pool. If the value within -128 to 127, they will be referred by two reference variable, but actually, there will be only one object.


Others will create objects on the heap as like any 'Other' objects.

So the == method, since it check the bit patterns of the object reference, will give the output like above....

|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Payel Bera
Greenhorn

Joined: Jan 28, 2010
Posts: 10
Thank you Tim and Abimaran for your prompt responses. Highly appreciate your explanations.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integer unboxing and == operator question
 
Similar Threads
Doubt in K&B Errata SCJP 5: topic Assignments "Wrapping & Unwrapping"
Integer comparison
Strange Behaviour Of Wrapper Class
KB 235
Strange Behaviour Of Wrapper Class