• 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

Clarification on == on wrapper objects

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A concept on wrapper objects says that ,

Two instances of following wrapper objects will always be == when their primitive values are same.

Byte
Boolean
Character (from 0 to 127)
Short and Integer (from -128 to 127)

Why is that two wrapper objects, if they are Character or Short or Integer will be == only if their values are in above mentioned range ?
And if values are out of range they will not satisfy condition of being == even if their respective values are equal .
 
A Agr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding to above query,

why this concept does not hold for other wrapper types like Long ,Double and Float ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Two instances of following wrapper objects will always be == when their primitive values are same.




Unfortunately, this statement is not true. I believe that you took this statement out of context.... The original context was probably about autoboxing, on how that is done via the valueOf() method, which uses an internal cache, which of course, gets the same wrapper object for the same value.

Without the context of autoboxing (or even the valueOf() method), the statement is completely not true.

Henry
 
A Agr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then why these 2 codes behave differently ?



Above code prints == equals .



Above code just prints equals and it does not print == .

Why ?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A Agr wrote:Adding to above query,

why this concept does not hold for other wrapper types like Long ,Double and Float ?




As another note, the Java Specification defines the cache, in terms of what range is required to be cached. This does *not* mean that anything out of the range is not to be cache. It is perfectly within the specification for a JVM implemenation to cache a larger range, or to cache other types not defined in the specification.

In fact, the Long type is also cache, but is not required by the specification for it to be so.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Then why these 2 codes behave differently ?



As mentioned, it is caused by autoboxing. If you don't use autoboxing they won't be equal (by ==, that is)... Try this...



Henry
 
A Agr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody explain it in detail ? Its not clear to me yet...
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Two instances of following wrapper objects will always be == when their primitive values are same.



Unfortunately, this statement is not true.

That depends on how you create the wrapper object. If you create it using valueOf() instead of the constructor, then it will cache instances with values fitting in the byte range (-127 - 128). So Integer.valueOf(10) == Integer.valueOf(10) will be definately true.

Not to mention that this is certainly not the best practice. When comparing Objects, you should always be using Object#equals().
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

That depends on how you create the wrapper object.



Well, since the original statement says "will *always* be ==", then doesn't if it "depends on how you create the wrapper object" mean that the statement is not true?

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A Agr wrote:Can somebody explain it in detail ? Its not clear to me yet...



You will need to elaborate here. I have already explained it... I can't elaborate in more detail, if you can't tell us what you don't understand.... "its not clear to me yet" doesn't tell us what to go into detail on.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic