• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Integer unboxing and == operator question  RSS feed

 
Greenhorn
Posts: 6
  • 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 "=="
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 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
  • 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: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 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...
 
Greenhorn
Posts: 10
  • 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: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 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??
 
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • 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.
 
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tim and Abimaran for your prompt responses. Highly appreciate your explanations.
 
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!