| Author |
Nan and ==
|
P Ventura
Ranch Hand
Joined: Jan 24, 2007
Posts: 42
|
|
Source: http://www.geocities.com/skmajji/Main.html why the following code prints "True"? It's a comparison between two constants!?
|
Objective: SCJP 1.5<br /><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html" target="_blank" rel="nofollow">API</a><br /> <blockquote><font size="1" face="Verdana, Arial">code:</font><hr><pre><font size="2"><br />Double n1 = Double.NaN; Double n2 = Double.NaN;<br /> n1.equals(n2) // true even though Double.NaN != Double.NaN<br />-0.0 == +0.0; // true<br />Double n1 = -0.0;Double n2 = +0.0;<br />n1.equals(n2) // false even though -0.0 == +0.0<br /></font></pre><hr></blockquote>
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Originally posted by P Ventura: ...why the following code prints "True"? It's a comparison between two constants!? ...
Actually, it prints "False." Yes, these are "constants," but they are constants representing things that are Not a Number. And because these are not real numbers, they cannot be compared in a real ordinal manner. That is, from a real number perspective, we can't say that one is greater than, less than, or equal to another. So these operations return false (with the exception of NaN != NaN, which must return true since NaN == NaN returns false). For example, consider that NaN might represent Math.sqrt(-1), or it might represent Math.sqrt(-987654). Neither of these are real numbers, but they're not equal either.
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
adam Lui
Ranch Hand
Joined: Sep 03, 2007
Posts: 186
|
|
i wonder if that will be even on the exam? it is not mentioned in the K&B or Apress.
|
boolean b = true;<br />System.out.println ("I believe in Java.<br />Java will make my dream come " + b);
|
 |
Brandon Bernie
Greenhorn
Joined: Nov 02, 2007
Posts: 9
|
|
|
It is covered in K&B. I just finished reading it and remember them talking about it. They do not cover it in much detail, however, it is just a simple concept that you should be able to remember.
|
 |
Mark Uppeteer
Ranch Hand
Joined: Mar 02, 2004
Posts: 159
|
|
pls tell us what page in K&B this is handled? as far as I remember this used to be on the exam for the 1.4 but isn't anymore for java 5. I wouldn't spend to much time on it...
|
I know where my towel is. (SCJP 5, OCPJWCD)
[Free Quiz Tips for a fun night with friends or family] Flash games
|
 |
Kelvin Chenhao Lim
Ranch Hand
Joined: Oct 20, 2007
Posts: 513
|
|
I'm pretty sure that none of the following will be tested in the SCJP exam, but just to add to the discussion: Other interesting (and probably non-intuitive) double behaviors: Double n1 = Double.NaN; Double n2 = Double.NaN; System.out.println(n1.equals(n2)); // prints true even though Double.NaN != Double.NaN System.out.println(-0.0 == +0.0); // prints true Double n1 = -0.0; Double n2 = +0.0; System.out.println(n1.equals(n2)); // prints false even though -0.0 == +0.0 [ November 14, 2007: Message edited by: Kelvin Lim ]
|
SCJP 5.0
|
 |
P Ventura
Ranch Hand
Joined: Jan 24, 2007
Posts: 42
|
|
|
I agree Kelvin.... ther're very non-intuitive and confusing. Let's hope that aren't in the exam!
|
 |
 |
|
|
subject: Nan and ==
|
|
|