Bloggers Wanted
We're looking for people to help with the main blog. If you are consistent, knowledgeable and you're into it, please drop me a note.
|
|
|
|
|
iphwin
Senior Boarder
Posts: 66
|
|
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. |
davidm
Senior Boarder
Posts: 65
|
|
)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 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. |
imported_baz
Senior Boarder
Posts: 69
|
|
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. |
mintgus
Senior Boarder
Posts: 76
|
|
)> 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. |
|
|
|