File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Float.NaN is equal to Float.NaN

 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,



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

Thanks

Sandy
 
agrah upadhyay
Ranch Hand
Posts: 579
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 481
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'(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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic