• 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 Statement in K&B Book

 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
on page no-236 k&B book say
To save memory two instances of the following wrapper objects will always be == when their primitive values are same
Boolean
Byte
Character
Short and Integer from -128 to 127

But according me Long can return equal(==) if value is in range -128 to 127.
See code below



Above code prints true
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's what the API for Long.valueOf(long) says could happen. (Long.valueOf(long) is used for autoboxing a long.)

If in doubt choose the Java 5.0 API documentation over K&B (but see my other post below)
[ July 24, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do separate autoboxing conversions sometimes return the same reference?


The Java Language Specification does not explicitly guarantee this behavior for long values within the range of a byte.

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As "wise" pointed out, there is no guarantee, that the implementation will cache Integers or Longs. If we take the word "always" into account, then K&B 5.0 is correct, because JLS says that the narrower wrapper types must behave like that.

In fact JLS says that Integers and Longs could cache values in the range of -32K to 32K (approx) if the implementation is meant for large memory systems.
 
reply
    Bookmark Topic Watch Topic
  • New Topic