aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Float.NaN is equal to Float.NaN 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 "Float.NaN is equal to Float.NaN" Watch "Float.NaN is equal to Float.NaN" New topic
Author

Float.NaN is equal to Float.NaN

Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
HI,



Can anyone explain
Why Does This Programs Prints false true false?

Thanks

Sandy


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
Mr.Sandeep
I Too 've Asked This Type Of question But Ranchers Referred Me Just To API In Sun Site.One Thing I Know That NaN is Not Equal To Anything Including Itself
#######################################################33
AgraH Upadhyay
A Sharma
Greenhorn

Joined: Sep 19, 2005
Posts: 11
One use of wrappers is to enable primitives to be used in collections which use the equals method of the object for their own methods. That's why equals of Float has been overridden to behave differently than == operator: just to make the collections work properly with wrapped Float.NaN
Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 481
See Float is the wrapper object.

Remember wrapper objects like strings are immutable means cannot change.

Also wrapper objects like strings override the equal method.

And in the code both the reference f1 and f2 create two different objects in the pool.so both doesnot reference to same object.So "== " is false.
And both the object are same so equals prints true.
so "==" always returns False for all wrapper objects and equal method returns true.

if i am wrong please correct it.
Joseph Clark
Ranch Hand

Joined: Sep 10, 2005
Posts: 48
'(f1==f2)' is false because the '==' operator checks to see if the references are identical, i.e., checks to see if the references (f1 f2) point to the same object. They do not, since you have created separate objects for both references.

'f1.equals(f2)' returns true since the .equals() method of class Object is overridden by class java.lang.Float to check if the objects are 'meaningfully equivalent'--in the way that String s="abc" and String t="abc" are equivalent.

'Float.NaN==Float.NaN' returns false because two *.NaN's are, by definition, not equivalent.

Float.NaN!=Float.NaN
returns true, as someone else pointed out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Float.NaN is equal to Float.NaN