This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Comparison operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparison operator" Watch "Comparison operator" New topic
Author

Comparison operator

Pawan Arora
Ranch Hand

Joined: Sep 14, 2008
Posts: 105
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

Joined: Mar 15, 2008
Posts: 210


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



Comparison happens with primitives only


SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
Pawan Arora
Ranch Hand

Joined: Sep 14, 2008
Posts: 105
Comparison happens with primitives only


So do you mean that it doesn't matter, which datatype type they are actually denoting?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817


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).


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Pawan Arora
Ranch Hand

Joined: Sep 14, 2008
Posts: 105
Thanks
 
wood burning stoves
 
subject: Comparison operator
 
Similar Threads
overloading
wrapper classes
Wrapper Class
Boxing and Equality of Double Object
simple doubt - Please help !!