aspose file tools*
The moose likes Beginning Java and the fly likes can't understand this..!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can Watch "can New topic
Author

can't understand this..!!

simpu ch
Greenhorn

Joined: Jun 21, 2004
Posts: 10
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.
Rovas Kram
Ranch Hand

Joined: Aug 08, 2003
Posts: 135
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

Joined: Aug 08, 2003
Posts: 135
Opps! I lied. longBitsToDouble returns a double not a Double.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8924
    
    9

"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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Rovas Kram
Ranch Hand

Joined: Aug 08, 2003
Posts: 135
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

Joined: Jun 21, 2004
Posts: 10
Thank u Joe ess and kram...for the explanation
Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 272
I think,

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

Thanks,
Guru
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8924
    
    9

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

Joined: Jun 21, 2004
Posts: 10
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.
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
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.)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Max Rahder
Ranch Hand

Joined: Nov 06, 2000
Posts: 177
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
Jack Kay
Ranch Hand

Joined: Aug 01, 2004
Posts: 62
== 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

Joined: Aug 01, 2004
Posts: 62
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

Joined: Aug 01, 2004
Posts: 62
OUTPUT:
Equal!
Equal!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can't understand this..!!