• 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

Wrappers

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I would like to ask why the following code works:



If we use the "==" or "!=" instead of "<" or ">" the code won't compile because two different wrapper type cannot be compared. Are the operators like "<" or ">" preform an automatic cast like compound assignments(e.g. +=)??


Kindest regards,

Kamila
 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because in case of "==" and "!=" the primitive value is boxed to its corresponding wrapper class object to perform the reference comparison and then the comparison is performed, which is not allowed between two non-comparable object types.
 
Kamila Jolan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swaraj thanks for the answer, I know that "==" and "!=" preform automatic boxing, but I am not sure what "<" and ">" doing with the wrappers?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kamila Jolan wrote: I know that "==" and "!=" preform automatic boxing, but I am not sure what "<" and ">" doing with the wrappers?



Actually, the "==" and "!=" comparison operators don't unbox the operands -- when used to compare two objects. These comparison operators will work for both primatives and objects, so when you use on two objects, it will do reference comparisons. On the other hand, if you compare one primative with an object, it knows that you want to do a value compare and will unbox the wrapper object.

In the case of the ">" and "<" comparison operators, it only works for primatives. There is no such a thing as one object being greater than another. So, it know that you want to do a value compare and will unbox both wrapper operands.

Henry
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
In the case of the ">" and "<" comparison operators, it only works for primatives. There is no such a thing as one object being greater than another. So, it know that you want to do a value compare and will unbox both wrapper operands.


And, in the above mentioned case, the both unboxed values are promoted to int and then compared.
 
Kamila Jolan
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for help! now I got it
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic