• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Double doubt

 
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wot 'll 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");
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it will print

FALSE
TRUE

False because the NaN represents a number between positive and negetive infinity which is fiddefrent for two different NaN's.

TRUE because NaN is represented in two ways...for eg: u can have two

you can read the specification given in Java API docs in Double class..
[ September 14, 2005: Message edited by: Amit Goel ]
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
False True
 
agrah upadhyay
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank U Mr. Amit,
I'm Still Not Clear.Wot 'd Difference Dos It Make.......


Agrah Upadhyay
B.Tech
3rd Year
SASTRA Deemed University,Tamilnadu
[ September 14, 2005: Message edited by: agrah upadhyay ]
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

you can read the specification given in Java API docs in Double class

Or, you can simply compile and run the code to find out yourself. "What will be the output?" is a far different question than "Why is the output what it is?".
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And Equal Constants wen equated Shud Equate 2 True



The behavior of Double.NaN is defined by the Java Language Specification: All numeric operations with NaN as an operand produce NaN as a result. As has already been described, NaN is unordered, so a numeric comparison operation involving one or two NaNs returns false and any != comparison involving NaN returns true, including x!=x when x is NaN.

Please make the extra effort to write out words such as "when", "should", and "to". The extra keystrokes won't cost much in the way of time, and the enhanced clarity will be appreciated by those communicating on a forum with international readership.

http://faq.javaranch.com/view?UseRealWords

Thanks!
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But I Wanna Know Y 'd OutPut false True Is there?


Therefore, you should ask "Why is the output 'False True'?", rather than "What is the output?". You can easily find out the output yourself, and that's not really the source of your confusion. The issue at hand is *why* the output is the way it is, and unrelated questions just get in the way...

Hope this helps!

http://faq.javaranch.com/view?UseRealWords
[ September 14, 2005: Message edited by: Steve Morrow ]
 
agrah upadhyay
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Mr. Steve And For Your Suggestion..
But What's The Reason For equals() which Return True





Agrah
B.tech
3rd Year
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But What's The Reason For equals() which Return True


Good question, easily answered by looking at the API:

If d1 and d2 both represent Double.NaN, then the equals method returns true, even though Double.NaN==Double.NaN has the value false.

It's an extremely important skill to learn to read the API and become familiar with the tools you will use to program Java. Java has an extensive set of documentation that you can even download for your convenience. These "javadocs" are indexed and categorized so you can quickly look up any class or method. Take the time to consult this resource whenever you have a question - you'll find they typically contain very detailed descriptions and possibly some code examples.

Java� API Specifications
Java� 1.5 JDK Javadocs
 
agrah upadhyay
Ranch Hand
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u Mr. Steve
But Can
Anyone Plz Let Me Know What Is The Reason Behind This "Optimization"?

Agrah
B.Tech
3rd Year
India
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But Can Anyone Plz Let Me Know What Is The Reason Behind This "Optimization"?



Please read the API for Double.equals(). Look at the last sentence in the description - it explains why equals() behaves the way it does.

http://faq.javaranch.com/view?UseRealWords
 
This is my favorite show. And this is my favorite 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
    Bookmark Topic Watch Topic
  • New Topic