SCSecA,SCNA,SCSA,SCWCD,SCJP
Hi
Why following program prints 10 and 1.0. Any Idea??
Originally posted by kittu anu:
Hi sun ram,
but when i ran this program it displays 10.0 and 10.0
-anu
Originally posted by sylvia weller:
Sun,
I got "10 and 10.0" - I think that's the same as you.
This is my theory: The return type of the an expression is determined at compile time. In the second expression (i>15 ? 10.0 : 10) the result is promoted up to the broader of the two possibilities, double (10.0) vs. int (10) => double. So you and I are both getting 10 promoted to 10.0.
In the first expression the compiler is able to fully evaluate the result - because there's no variable data. So I think there's a good chance, in your compiler and mine, that the compiler just inlined the result - the same false 10 as in expr2, but no promotion applied because the result was known at compile time. (The is similar to how C macro works).
For others who are not getting same output as we are, I would think their compilers are not inlining the first expression but evaluating both via promotion.
Just a theory. Anyone, please correct if it is wrong -- don't want to confuse people.
Sylvia
Rob
SCJP 1.4
Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Originally posted by Younes Essouabni:
I am not able to test the code here!! But in my sense the answer should be 10.0 and 10.0
Doesn't those optimization go against the portability of java. Shouldn't all compiler react the same way???
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |