• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Boxing Integer, Short, Byte and Char

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Can any one Explain in detail whats happening in BOLD lines.

is it comparing referance or value ?

Thanks
Saqib

[ April 12, 2007: Message edited by: saqib sarwar ]
[ April 12, 2007: Message edited by: saqib sarwar ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In this line, references are being compared.

But because bd and be refer to Boolean objects created by autoboxing true, they refer to the same object.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, there is a unfortunately a rule that you may have to commit to memory:

When 2 wrapper instances created through autoboxing will always be == when there primitive values are the same. This rule only applies to the following:

Boolean
Byte
Character from \u0000 to \u007f (127 in decimal)
Short from -128 to 127
Integer from -128 to 127

I believe this is because the designers of Java wanted to optimise memory management by having the references refer to the same object. Similar to the String literal pool I guess.

If anyone has a simple way of remembering this rule, please let me know. Or maybe someone may be able to explain exactly why it is only the above circumstances that apply.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from the Java Language Specification.

If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

Discussion

Ideally, boxing a given primitive value p, would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rules above are a pragmatic compromise. The final clause above requires that certain common values always be boxed into indistinguishable objects. The implementation may cache these, lazily or eagerly.

For other values, this formulation disallows any assumptions about the identity of the boxed values on the programmer's part. This would allow (but not require) sharing of some or all of these references.

This ensures that in most common cases, the behavior will be the desired one, without imposing an undue performance penalty, especially on small devices. Less memory-limited implementations might, for example, cache all characters and shorts, as well as integers and longs in the range of -32K - +32K.


Boxing Conversion
[ April 12, 2007: Message edited by: Keith Lynn ]
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting yes. Very important? I don't see how this is any more important than any of the other threads in this forum.

As a general rule, please avoid words like "importtant" or "urgent" in youir thread titles - if anything it will tend to make people avoid answering your question. Everyone here is a volunteer, and every answer is created because someone decided to spend their precious time for free!
 
saqib sarwar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steven Young & keith Lynn For Nice explaination.

specially the background reasoning provided by keith lynn.



And for Bert Bates, "very important?" means this concept is very important for all in my point of view. Well, Bert i will try to make my Topic more a appropriate in future.

Thanks to All
 
reply
    Bookmark Topic Watch Topic
  • New Topic