Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Double doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Double doubt" Watch "Double doubt" New topic
Author

Double doubt

agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
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");
Amit Goel
Ranch Hand

Joined: Dec 07, 2000
Posts: 50
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 ]

Amit<br /> <br />The Less I have, The more I gain..Off the Beaten Path, I Reign.
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
False True
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
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 ]
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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

Joined: May 22, 2003
Posts: 657

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

Joined: May 22, 2003
Posts: 657

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

Joined: Sep 01, 2005
Posts: 579
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

Joined: May 22, 2003
Posts: 657

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

Joined: Sep 01, 2005
Posts: 579
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

Joined: May 22, 2003
Posts: 657

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Double doubt