File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integer unboxing and == operator question

 
randy pausch
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just search in the forum for "integer pool" or "integer 127" and you'll get a result like this...
 
randy pausch
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry...I shud have searched before asking.

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

Thanks Ankit.
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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....
 
Payel Bera
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tim and Abimaran for your prompt responses. Highly appreciate your explanations.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic