Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Nan and ==

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: http://www.geocities.com/skmajji/Main.html

why the following code prints "True"? It's a comparison between
two constants!?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i wonder if that will be even on the exam?
it is not mentioned in the K&B or Apress.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 159
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
     
    P Ventura
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I agree Kelvin.... ther're very non-intuitive and confusing. Let's hope that aren't in the exam!
     
    WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic