Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparison operator

 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I tried to compare to primitive values like

Here println method returning the output as true instead of false. I wanna know why is that so, 'cause as far as I'm concerned both primitive values will implicitly convert into wrapper classes, Double and Integer for comparison, and two wrapper classes cannot be considered equal.
Thanks in advance for help.
[ October 28, 2008: Message edited by: Pawan Arora ]
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm concerned both primitive values will implicitly convert into wrapper classes



Comparison happens with primitives only
 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comparison happens with primitives only


So do you mean that it doesn't matter, which datatype type they are actually denoting?
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

as far as I'm concerned both primitive values will implicitly convert into wrapper classes


That's when you assign a primitive to a wrapper of that type (say int to Integer reference) with JDK 5 onwards, called boxing (or auto boxing). And the reverse also possible (unboxing) when you assign wrapper type reference to premitive reference.

Here "==" operator checks the content(value) of what the references refers to. In this case both equal (value 42).
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What actually happens is that the int value is promoted to a double value for comparison: http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#170983
 
Pawan Arora
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic