• 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:

Question on Float.NAN

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Float f1=new Float(Float.NAN), f2=new Float(Float.NAN)
Why is f1.equals(f2) true and why is Float.NAN==Float.NAN false?
Dosn't equals(of Float) look at the value of f1 which is Float.NAN and isn't it equivalent to doing
f1==f2?
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Float.equals method compares the bit patterns of the primitive float value. By comparing the bit patterns the unusual behavior of NaN is avoided. The javadoc for the Float.equals method covers this issue in detail. The source code for Float.equals is as follows.
 
Hemal Mehta
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This clears it up. I assume it's the same for all other Number wrapper classes.
reply
    Bookmark Topic Watch Topic
  • New Topic