• 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

Doubt with NaN

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source SK Majji Q6

The following code will print

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");

A) True
True

B) True
False

C) False
True

D) False
False
Ans: C

Doubt: why are we getting true for line 10?

pls help,
regards,
Gitesh
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gitesh

The Ans is correct.
The reason is the Double class overrides the equals() method.
so it will compare the values.

since both the objects containing Double.NaN it returns true.
like
Double d1 = new Double(12.2);
Double d2 = new Double(12.2);
if( d1.equals(d2)) // true.

the question why if( Double.NaN == Double.NaN ) returns false is unknow, you have to check with JLS.
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wrapper and Strings implements method equals for standard.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My doubt is both a and b contain NaN, and if NaN!=NaN then how
( a.equals(b) )gives true??

Pls help,

regards,
Gitesh
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at the API documentation of the equals() method in class Double. It explains why it works like this:

Note that in most cases, for two instances of class Double, d1 and d2, the value of d1.equals(d2) is true if and only if

d1.doubleValue() == d2.doubleValue()

also has the value true. However, there are two exceptions:

  • If d1 and d2 both represent Double.NaN, then the equals method returns true, even though Double.NaN==Double.NaN has the value false.
  • If d1 represents +0.0 while d2 represents -0.0, or vice versa, the equal test has the value false, even though +0.0==-0.0 has the value true.

  • This definition allows hash tables to operate properly.


    [ September 04, 2007: Message edited by: Jesper Young ]
     
    Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic