Check out Manning's Countdown to 2014. Use discount code crdotd14 all month for 50% off every deal.
Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# Wrapper

Robbie kyodo
Ranch Hand

Joined: May 05, 2003
Posts: 97
Hi
can somebody explain the return statement to me ?

class D {
static boolean m(double v) {
return(v != v == Double.isNaN(v));
}
public static void main (String args[]) {
double d1 = Double.NaN;
double d2 = Double.POSITIVE_INFINITY;
double d3 = Double.MAX_VALUE;
System.out.print(m(d1) + "," + m(d2) + "," + m(d3));
}
}

SCJP 2 1.4
Robbie kyodo
Ranch Hand

Joined: May 05, 2003
Posts: 97
Its ok, I understand,
pinky yadav
Ranch Hand

Joined: Jun 17, 2002
Posts: 44
!= and == have same precedence so they are evaluated from left to right if both occure in same expression.
here in this expression: v != v == Double.isNaN(v)
first v!=v is evaluated which results in boolean.
after this == is eveluated.
for readability consider this as
(v != v) == (Double.isNaN(v))
hope this is helpful
Pinky
Robbie kyodo
Ranch Hand

Joined: May 05, 2003
Posts: 97
thanks Pinky
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Thank you Pinky.
This question demonstrates that NaN is never equal to itself. For example, you can not check to see if a variable, x, is equal to NaN using the expression (x == NaN) because the result will always be false. Instead, you should check to see if a variable is equal to NaN by passing it as an argument to the isNaN method.
The isNaN method just returns the result of the expression (v != v). Therefore, the expression
((v != v) == Double.isNaN(v)) will always be true.

Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Ruosong Wei
Greenhorn

Joined: May 14, 2003
Posts: 1
then, i still don't get it, why the second part of the expression is not evaluated. that is :
Double.isNaN(v)? if both v!=v and Double.isNaN(v) are evaluated, and we put NaN into "v", the result
should be something like true == true , and the result will definitely be true.
By the way Double.MAX_VALUE is also not a number?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch, Rouosong.
Dan's explanation has already agreed with your post.
Double.MAX_VALUE is not NaN.

SCJP2. Please Indent your code using UBB Code

subject: Wrapper
Similar Threads
Question for Dan Wrapper Class
Dan's exam question - Double.isNaN
Wrappers
Wrapper Classes.
Dans' Wrapper Question