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

S2.equals(7) is false, but (S2 == 7) is true. Short S2 = new Short((short)7); Why?

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

I've been looking at some conditionals in preparation for the SCJP exam. Can you explain the following result that I observed in an actual program.



RESULT:

false
true

I understand why the first conditional is false, but why is the second conditional true. S2 is a Short wrapper, and 7 is an integer primitive.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Numbers between -128 and 127 will give true when using wrappers and the equality ==.
Now, if you want to know why is the implementation that way , i will recomend you to look the jls. You don't need to know the why of the implemenation for the scjp but basically it has something to do with memory optimization.
 
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:
  • Report post to moderator
When you compare a wrapper and primitive value using any comparison operator like == or != or > ..... then the wrapper is first unboxed for the comparison. So S2 == 7 becomes S2.shortValue() == 7 and that's why its true...
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
[rant]I hate auto-boxing/unboxing and all the confusion which comes with it[/rant]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

armando fonseca wrote:Numbers between -128 and 127 will give true when using wrappers and the equality ==.


But that is in this case not why the second answer is 'true'!

The fact that == returns true in some cases is because classes like Integer have a cache of Integer objects for all the values between -128 and 127, and if you call Integer.valueOf(int) with a value in that range, the method will return an Integer object from the cache. Note that autoboxing calls Integer.valueOf(int) to automatically box an integer.

In the case above, S2 is created with the new operator. It is not an object from the cache - even though the value is 7. When you compare it to an object from the cache, the result will be false.
 
armando fonseca
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Jesper Young wrote:

armando fonseca wrote:Numbers between -128 and 127 will give true when using wrappers and the equality ==.


But that is in this case not why the second answer is 'true'!

The fact that == returns true in some cases is because classes like Integer have a cache of Integer objects for all the values between -128 and 127, and if you call Integer.valueOf(int) with a value in that range, the method will return an Integer object from the cache. Note that autoboxing calls Integer.valueOf(int) to automatically box an integer.

In the case above, S2 is created with the new operator. It is not an object from the cache - even though the value is 7. When you compare it to an object from the cache, the result will be false.



You are right... I'm a shame.... just keep it between us.
regards,
Armando.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
No problem, it isn't a shame that you have to learn all those tricky details!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I guess this s2 == 7 is evaluating to true because of Unboxing and not because of the caching of -128 to 127.
The reason being, s2 == 200 also evaluates to true.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Yeah true and has been already clearly explained by Ankit above. I think caching comes into play when we compare 2 objects with the values and well i don't like the sound of the caching that much....i like referring it as pool like the Strings do. Well wrappers and Strings have this special thing called pool well i don't think not all wrappers do... which ones hmm readers guess
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Closing this thread. Continue the discussion there.
 
I hired a bunch of ninjas. The fridge is empty, but I can't find them to tell them the mission.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic