• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Float.NaN Query

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

Another example taken from http://devesh2k1.googlepages.com/home

===================================================================

Float f1 = new Float(Float.NaN);
Float f2 = new Float(Float.NaN);

System.out.println(f1 == f2); //Line 1 - Prints false
System.out.println(f1.equals(f2)); //Line 2 - Prints true
System.out.println(Float.NaN == Float.NaN); //Line 3 - Prints false


Float f3 = 0.0f;
Float f4 = -0.0f;
System.out.println(f3.equals(f4)); //Line 4 -Prints false
System.out.println(-0.0f == 0.0f); //Line 5 -Prints true


I understand that Line 1 prints false as f1 and f2 are two different objects. Line 2 is true as the contents of the objects are the same, hence true.

I am unsure of why Line 3 is false, please can someone explain?

Line 4 is false as the contents of each object are different.

Line 5 is true as this is an exceptional case stated by the javadoc.

Thanks
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Float.NaN return Float, if so, it would return false just like your line # 1

Thanks,
Mohammad
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are some explanations here:
https://coderanch.com/t/269299/java-programmer-SCJP/certification/NAN
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javadoc says that no two NaN values are the same, this is why line 3 prints false..
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
equals() method special conditions
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic