wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Float.NaN Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Float.NaN Query" Watch "Float.NaN Query" New topic

Float.NaN Query

Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
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.


My Path to SCJP Certification My Path to SCWCD Certification
Mohammad Khan
Ranch Hand

Joined: Sep 23, 2008
Posts: 37
Float.NaN return Float, if so, it would return false just like your line # 1

Rashmi Jaik
Ranch Hand

Joined: Oct 04, 2008
Posts: 50
There are some explanations here:
Ankit Garg

Joined: Aug 03, 2008
Posts: 9465

javadoc says that no two NaN values are the same, this is why line 3 prints false..

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Vipul Prajapati

Joined: Aug 29, 2008
Posts: 9
equals() method special conditions
I agree. Here's the link: http://aspose.com/file-tools
subject: Float.NaN Query
jQuery in Action, 3rd edition