• 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

Double Equals

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1: Double a = new Double(Double.NaN);
2: Double b = new Double(Double.NaN);
3:
4: if( Double.NaN == Double.NaN )
5: System.out.println("True");
6: else
7: System.out.println("False");
8:
9: if( a.equals(b) )
10: System.out.println("True");
11: else
12: System.out.println("False");
The answer is
False
True.
I don't understand why the second case is true. It is given that Double.NaN is never equal to Double.NaN.
Can anybody explain why is it so!!!.
Thanx
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the primitives behave according to IEEE-754 standard, but
the Wrapper has to behave according to the contract of equals method in Object class. This is even clearer in:
Integer iw = new Integer(10);
Float fw = new Float(10.0f);
if (iw.equals(fw))
System.out.println("Equal");
The equals could have returned true. But the contract of equals doesn't allow it. If you check,
if(a.doubleValue() == b.doubleValue())
System.out.println("True doubleValue");
else
System.out.println("False doubleValue");
it returns false.
 
reply
    Bookmark Topic Watch Topic
  • New Topic