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:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Doubt with ==

Ranch Hand
Posts: 274
• Number of slices to send:
Optional 'thank-you' note:
Given:
1. public class Foo {
2. private int val;
3. public foo(int v) (val = v }
4. public static void main (String [] args) {
5. Foo a = new Foo (10);
6. Foo b = new Foo (10);
7. Foo c = a;
8. int d = 10;
9. double e = 10.0;
10. }
11. }
Which three logical expressions evaluate to true? (Choose Three)
A. (a ==c)
B. (d ==e)
C. (b ==d)
D. (a ==b)
E. (b ==c)
F. (d ==10.0)
Ans: A,B,F

Doubt How B is correct, as it involves a double value? Also does it takes only the non-fractional part for comparison?

pls help
regards,
gitesh

author
Posts: 23951
142
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Gitesh Ramchandani:

Doubt How B is correct, as it involves a double value? Also does it takes only the non-fractional part for comparison?

No... when a int is compared with a double, the int is cast to a double for the comparison. Since, a value of 10, cast to a value of 10.0 without any lost of precision, the comparison is 10.0 to 10.0, which should be equal.

Thanks,
Henry

Ranch Hand
Posts: 35
• Number of slices to send:
Optional 'thank-you' note:
when you compare numeric values internally it will promotes lower to higher version.
0==0.0
in this case 0 is converted into double as 0.0
then the compiler will perform comaprision.

Gitesh Ramchandani
Ranch Hand
Posts: 274
• Number of slices to send:
Optional 'thank-you' note:
thanks for help.