• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Doubt in K&B Errata SCJP 5: topic Assignments "Wrapping & Unwrapping"

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the errata, it's given :

236....clarify...Sentence before bullet points s/b: In order to save memory, two instances of the following wrapper objects, created through autoboxing, will always be equal...

236....clarify...Add this sentence: When == is used to compare a primitive to a wrapper object, the wrapper object will be unwrapped to its primitive value, and the comparison will be made between the two primitives' values.

That's fine. But this still doesnt explains as to why this happens only upto the range of -128 to +127 for short & int. Why not their entire range & the others like float & double are included in this.

I just cant figure out as to what complications or problems might occur if the entire range & other primitive wrappers are also included.

Someone please help me out on this....
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

236....clarify...Add this sentence: When == is used to compare a primitive to a wrapper object, the wrapper object will be unwrapped to its primitive value, and the comparison will be made between the two primitives' values.



Hi Sen,




Thanks and Regards,
cmbhatt
 
Sourin K. Sen
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Bhatt,



The above code prints :
true
true
false
true

What i wanted to ask is if due to autoboxing the primitive wrapper references point to the same object when the values are same & in the range of -128 tp +127, then what harm will it do if the range is increased to the range of int & all other primitive wrapper classes are also included ??

i.e, what wud go wrong or what harm will it do if the following statement from the above code returns true :

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sen,


i.e, what wud go wrong or what harm will it do if the following statement from the above code returns true :

code:

System.out.println(i3 == i4);


It is what language designer thought to be. To save memory to
store values below 128 they made the provision that if Byte, Character,
Short and Integer wrapper classes store values below 128 -128 to 127 they
will be "==". In the practical ground it is found true for long also but
Java Language Specification doesnt't say anything about this.

Finally "it is all about to save the memory".

Thanks and Regards,
cmbhatt
 
Sourin K. Sen
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you still didnt answer my question properly.
if it's all about saving memory, then why the bigger values have been left out of this??
Had they been included in this, it could have saved even more memory i suppose, isnt it??
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic