This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Float.NaN Query" Watch "Float.NaN Query" New topic
Author

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.

Thanks


Santiago
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

Thanks,
Mohammad
Rashmi Jaik
Ranch Hand

Joined: Oct 04, 2008
Posts: 50
There are some explanations here:
http://www.coderanch.com/t/269299/java-programmer-SCJP/certification/NAN
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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
Greenhorn

Joined: Aug 29, 2008
Posts: 9
equals() method special conditions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Float.NaN Query
 
Similar Threads
NaN values..
Float.NaN is equal to Float.NaN
Invoking equals method on float object
Question on NAN
Understanding NaN