My Profile

Keep Up to Date:
Blog RSS
Blog
Forum RSS
Forum
Search

Buy & Sell

Used (Like New) $20

Post New Topic Post Reply
Posted 1 Year, 3 Months ago
iphwin
Senior Boarder
Posts: 66
graphgraph
User Offline
 
What should be the output of the following java program(guess without running the program).

class test { public static void main(String argshat should be the output of the following java program(guess without running the program).

class test { public static void main(String args[]) { int i=10; System.out.println(i<11?9:9.0); }

Scroll down for the answer
The administrator has disabled public write access.
Posted 1 Year, 3 Months ago
davidm
Senior Boarder
Posts: 65
graphgraph
User Offline
 
)What should be the output of the following java program(guess without )running the program). ) )class test ){ ) public static void main(String args[]) ) { ) int i=10; ) System.out.println(i&lt;11?9:9.0); ) } )} ) )Scroll down for the answer )&gt; )&gt; )&gt; )&gt; )&gt; )&gt; )&gt; )&gt; )&gt; )&gt; )&gt; )&gt; )&gt; )&gt; )&gt; )&gt; )&gt; )&gt; )&gt; )&gt; )&gt; )9.0

You forgot to mention why:

The compiler needs to know the type of &#039; &lt;X&gt; ? 9 : 9.0 &#039;, and it gets this from combining the types of the two branches (if possible). An int and a float combine to a float, so What should be the output of the following java program(guess without )running the program). ) )class test ){ ) public static void main(String args[]) ) { ) int i=10; ) System.out.println(i<11?9:9.0); ) } )} ) )Scroll down for the answer )> )> )> )> )> )> )> )> )> )> )> )> )> )> )> )> )> )> )> )> )> )9.0

You forgot to mention why:

The compiler needs to know the type of ' <X> ? 9 : 9.0 ', and it gets this from combining the types of the two branches (if possible). An int and a float combine to a float, so 9 becomes 9.0.

Try changing the '9.0' in the program to ''9.0'' and see what happens. Also, try changing the 9.0 to 8.0; you should still get 9.0
The administrator has disabled public write access.
Posted 1 Year, 3 Months ago
imported_baz
Senior Boarder
Posts: 69
graphgraph
User Offline
 
I haven't actually done it, but I don't think that changing '9.0' to ''9.0'' will work. This is because integers can be promoted to floats, but not to strings (objects).

You can do: double d = 9; // promotion is automatic

But you can't do any of: int i = '9.0'; or Object o = 9; or String s = 9;
The administrator has disabled public write access.
Posted 1 Year, 3 Months ago
mintgus
Senior Boarder
Posts: 76
graphgraph
User Offline
 
)> Try changing the '9.0' in the program to ''9.0'' and see what happens. )> Also, try changing the 9.0 to 8.0; you should still get 9.0 ) )I haven't actually done it, but I don't think that changing '9.0' to )''9.0'' will work. This is because integers can be promoted to floats, )but not to strings (objects). ) )You can do: )double d = 9; // promotion is automatic ) )But you can't do any of: )int i = '9.0'; or Object o = 9; or String s = 9;

Exactly. That's what I meant with 'and see what happens' What should happen is you get a compiler error about mixing types or something similar.
The administrator has disabled public write access.
 
Copyright © 2006 - Jan 2009 Fun Quizzes Club