Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Float.NaN Query

 
Santiago Bravo
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • 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
 
Mohammad Khan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Float.NaN return Float, if so, it would return false just like your line # 1

Thanks,
Mohammad
 
Rashmi Jaik
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some explanations here:
http://www.coderanch.com/t/269299/java-programmer-SCJP/certification/NAN
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javadoc says that no two NaN values are the same, this is why line 3 prints false..
 
Vipul Prajapati
Greenhorn
Posts: 9
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic