• 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

Comparison operator

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic