• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Float.NaN is equal to Float.NaN

 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,



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

Thanks

Sandy
 
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic