• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Integer unboxing and == operator question

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Tim and Abimaran for your prompt responses. Highly appreciate your explanations.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic