• 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 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

Why i am getting different output!!!!!

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Why following program prints 10 and 1.0. Any Idea??
-SR
public class Test {

private static void main(String[] args ) {
System.out.println((false)?10.0:10);
int i=10;
System.out.println((i>15)?10.0:10);
}
}
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sun ram,
but when i ran this program it displays 10.0 and 10.0
-anu
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
when i also executed the prgram it gives 10.0
and 10.0.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i also got the answers as 10 and 10.0
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi
Why following program prints 10 and 1.0. Any Idea??


No, it is printing 10,10.0. Please check it again...
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes that is correct.
numeric promotion will be applied. ie integers would be converted to float and then condition evaluated and it is false in both cases.
so second value after the colon is printed as 10.0 and 10.0

Originally posted by kittu anu:
Hi sun ram,
but when i ran this program it displays 10.0 and 10.0
-anu

 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For complete information about the ternary ?: operator please check out 15.25 Conditional Operator ? :.
HIH
[ January 27, 2002: Message edited by: Valentin Crettaz ]
 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


I am confused I just read the JLS that was poted and according to that it looks like numeric promotion should take place, meaning I should get 10.0, 10.0, but I also get 10, 10.0. Are you saying the different behaviors of the JVMs are the cause of the different output? If this came up on a test, what should the anser be? I would think in both cases it should be 10.0 and 10.0 but it isn't. This has me puzzled.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it has to do with the fact that in the first expression the condition is a constant boolean expression (false) while in the second it's not. Maybe some compiler are treating the code differently depending on some optimizations.
It makes perfect sense that if the compiler already knows that the conditional expression is false, then the third expression will be taken without applying numeric promotion...
Consider the following code where both conditional expression are not constant.

The output is 10.0 10.0 because whatever the compiler is, no optimizations are possible in this case.
Why do you guys think?
[ January 27, 2002: Message edited by: Valentin Crettaz ]
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Val, your explanation makes sense to me. The compiler does a pretty good job optimizing things, especially constant expressions, so whenever contants are being used, it is good to keep in mind how the optimizer will treat it.

Rob
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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???


I was wondering the same thing. Not that I believe optimization goes against the portability of java but I would think it should act consistant in similar situations in how it tries to optimize things. Appratently that isn't the case here?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic