• 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

can't understand this..!!

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

This is a question from majji's mock exam

Question 6



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

The answer is C
I couldn't figure out why "if( Double.NaN == Double.NaN )" gives false
Someone please clarify it...
Thanks in advance,
simpu.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Double.NaN is equivalent to Double.longBitsToDouble which returns a new Double object. The '==' operator is comparing references not values; therefore it's false.
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Opps! I lied. longBitsToDouble returns a double not a Double.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"NaN" stands for "Not a Number" and is used to indicate that a particular double is not valid. Think about it, if a value is "not a number", how can it be equal to anything? The Double class provides a method, isNaN(), for evaluating the NaN condition. Furthermore, floating point numbers are approximate values, so using "==" to compare them is a Bad Idea.
 
Rovas Kram
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The special NaN value is not equal to any number including itself(whatever). Use Double.isNan() Method.
Reference: Java in a Nutshell, 3rd Edition, O'Reilly.
 
simpu ch
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank u Joe ess and kram...for the explanation
 
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think,

the references are different. Since, NaN is a static field in Double.

Thanks,
Guru
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gurumurthy Ramamurthy:

the references are different. Since, NaN is a static field in Double.



No. A static field will have the same value or reference in every instance of a class. The VALUE of NaN is not equal to anything, including itself. That's because it is Not a Number. For example, divide 0 by 0. If you do 0/0 with ints you get an ArithmeticException. With double, you get NaN, meaning the result is invalid.
 
simpu ch
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fine Everyone,
My doubt about the first question is clarified,but there is another one....
In these lines..

9: if( a.equals(b) )
10: System.out.println("True");
11: else
12: System.out.println("False");

How could the NaN in "a" be equal to "b"? :roll:
and so..How could it be returning "true"instead of "false?

simpu.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The use of NaN is not relevant to this discussion.

You are comparing two objects. They have the same value, but they are two separate objects. Therefore, (a == b) returns false because a and b reference different objects. a.equals(b) returns true because the two objects have the same value.

(The use of NaN as the value fot the Double objects doesn't matter; you'd get the same results if you used 16.3 or anything else.)
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Max Rahder:
The use of NaN is not relevant to this discussion



Sorry, but wrong. a and b, here, are never compared with "==". They're compared with equals(), which, for any other Double object, would return "true" when applied reflexively. "==" is only used to compare values of type double in this example.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Java Language Specification requires that NaN is not equal to anything including itself, so NaN == NaN is false. The Double.equals method interprets the 64 bit double value as a 64 bit long value before comparing the values contained by two instances of type Double. As a long value, the 64 bit pattern of NaN has no special meaning; it's just another 64 bit value. Although that implementation of Double.equals may seem confusing, it is required to make hashtables work properly.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops. You're right of course, Double.NaN is of type "double". Here's what I found in the language spec:

"NaN is unordered, so the numerical comparison operators <, <=, >, and >= return false if either or both operands are NaN (�15.20.1). The equality operator == returns false if either operand is NaN, and the inequality operator != returns true if either operand is NaN (�15.21.1). In particular, x!=x is true if and only if x is NaN, and (x<y) == !(x>=y) will be false if x or y is NaN"
-Java Language Specification, Second Edition
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
== is for comparing PRIMITIVES(double, boolean, int, float) & comparing if two objects are referring to the same object in memory
.equals is for comparing OBJECTS having the same value(String, Double, Boolean, Float, Integer, Object, InstanceOfAnyClass)

---------------------------

Oh jeez I just reread the first post...
Ok they both should be TRUE because you're comparing primitives Ok in the first one (since they both should be equal), and you're comparing objects Ok in the second one (since they should be equal too). Hmmm!

[ August 01, 2004: Message edited by: Jack Kay ]
[ August 01, 2004: Message edited by: Jack Kay ]
[ August 01, 2004: Message edited by: Jack Kay ]
[ August 01, 2004: Message edited by: Jack Kay ]
[ August 01, 2004: Message edited by: Jack Kay ]
[ August 01, 2004: Message edited by: Jack Kay ]
[ August 01, 2004: Message edited by: Jack Kay ]
lol....
[ August 01, 2004: Message edited by: Jack Kay ]
 
Jack Kay
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joe Ess:

No. A static field will have the same value or reference in every instance of a class. The VALUE of NaN is not equal to anything, including itself. That's because it is Not a Number. For example, divide 0 by 0. If you do 0/0 with ints you get an ArithmeticException. With double, you get NaN, meaning the result is invalid.



Oh ok so that's why the first one is false. Since their values are not valid, there can't be no way to compare! It's like comparing these:

if((13.0/0.0) == (4.0/0.0)){
System.out.println("This isn't going to happen");
}
[ August 01, 2004: Message edited by: Jack Kay ]
 
Jack Kay
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OUTPUT:
Equal!
Equal!

 
You've gotta fight it! Don't give in! Read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic