Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# Double doubt

Ranch Hand
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
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 ]

vidya sagar
Ranch Hand
Posts: 580
False True

Ranch Hand
Posts: 579
Thank U Mr. Amit,
I'm Still Not Clear.Wot 'd Difference Dos It Make.......

B.Tech
3rd Year
[ September 14, 2005: Message edited by: agrah upadhyay ]

Steve Morrow
Ranch Hand
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
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
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 ]

Ranch Hand
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
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

Ranch Hand
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
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