This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparison operator  RSS feed

 
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 ]
 
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?
 
Bartender
Posts: 4042
47
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).
 
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
Boost this thread!