aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Invoking equals method on float object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Invoking equals method on float object" Watch "Invoking equals method on float object" New topic
Author

Invoking equals method on float object

Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi,
Please go through the below code,

I am getting the output as false, true.
Why i am getting the false at line number 1?
Is it any way related to (-0.0==+0.0 )
That means, equals method will not check exactly the object value. Is it??

Thanks..


Narasimha
Richard Quist
Ranch Hand

Joined: Feb 18, 2004
Posts: 96
This behavior is by design. According to the java API doc for the equals method of the Float class:
Note that in most cases, for two instances of class Float, f1 and f2, the value of f1.equals(f2) is true if and only if
f1.floatValue() == f2.floatValue()
also has the value true. However, there are two exceptions:
If f1 and f2 both represent Float.NaN, then the equals method returns true, even though Float.NaN==Float.NaN has the value false.
If f1 represents +0.0f while f2 represents -0.0f, or vice versa, the equal test has the value false, even though 0.0f==-0.0f has the value true.
This definition allows hash tables to operate properly.

[ March 16, 2004: Message edited by: Richard Quist ]

Rich
SCJP 1.4
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi Richard,
Thanks a lot for nice explanation.

Thanks..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Invoking equals method on float object