This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals method behavior of Float Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals method behavior of Float" Watch "equals method behavior of Float" New topic
Author

equals method behavior of Float

reji singh
Ranch Hand

Joined: Apr 06, 2009
Posts: 52
Hi All,

As per JDK docs

If f1 represents +0.0f while f2 represents -0.0f, or vice versa, the equal test has the value false, //1

even though 0.0f==-0.0f has the value true. //2

Doubt: I agree with statement 1 as -0.0f is not equal to 0.0f but why statement 2 is true? It should not be true as per equality of Objects and primitives.


Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

Doubt: I agree with statement 1 as -0.0f is not equal to 0.0f but why statement 2 is true? It should not be true as per equality of Objects and primitives.


Statement 2 is true because that is how it is defined by the IEEE spec. In a comparison, negative zero is equal to positive zero.


As for why statement 1 is false, meaning why it violates IEEE specs -- this was done so maps and sets don't consider positive and negative zero as duplicates of each other.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tom Kowalski
Ranch Hand

Joined: Feb 17, 2009
Posts: 72
And don't forget that result depends on whether the variables are Float or float.


SCJP, SCWCD, OCUP
reji singh
Ranch Hand

Joined: Apr 06, 2009
Posts: 52
Thanks Henry and Tomasz
 
 
subject: equals method behavior of Float
 
Similar Threads
NaN values..
doubt: equals&==
Simple question
Invoking equals method on float object
NaN