Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Double.NaN comparison using equals method

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to compare Double.NaN value,which always returns false when compared with another Double.NaN. equals method from Object class is being overridden by Wrapper classes and checks for value equality. According to this, why does compare 3 return true? how is equals method comparing the value represented by Double Object?


//DoubleValue.java

import java.lang.*;

public class DoubleValue{

public static void main(String[] arg){

Double a = new Double(Double.NaN);
Double b = new Double(Double.NaN);
//System.out.println("Double.Nan value in double "+a.doubleValue() +

b.doubleValue());

if(a.doubleValue() == b.doubleValue()) //compare 1
System.out.println("doubleValue method:Equal");
else
System.out.println("doubleValue method:not Equal");

if(Double.NaN == Double.NaN) //compare 2
System.out.println("Double.Nan is equal to Double.NaN");
else
System.out.println("Double.Nan is not equal to Double.NaN");

if(a.equals(b))//compare 3
System.out.println("equal");
else
System.out.println("Not equal");
}

}
[ May 11, 2006: Message edited by: pramila ch ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by pramila ch:
...why does compare 3 return true? how is equals method comparing the value represented by Double Object? ...


According to the API documentation for Double, "If d1 and d2 both represent Double.NaN, then the equals method returns true, even though Double.NaN==Double.NaN has the value false."
 
Pramila Chinguru
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marc,
Can you please point me to that link. I searched in API specification, but failed to get to exact point.
Thanks
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scroll down to the "equals" method defined for Double...

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Double.html
reply
    Bookmark Topic Watch Topic
  • New Topic