This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
class XTC20 { public static void main ( String [ ] args ) { // ---> line 1 int x = true ? 10 : 20 ; // ---> line 2 System . out . println ( x ) ; // ---> line 3 } }; What does code in line 2 mean?
booleanExpression ? expression1 : expression2; expression1 is your answer if the booleanExpression is true else it is expression2. Its a conditional statement, an if else statement. [ November 08, 2002: Message edited by: kevin comario ]
It means: if condition is true, than asign the first value after the question mark to the variable, else the second. The condition is BEFORE the question mark, so the layout looks like this: int x = <condition> ? <assign value to x if condition is true> : <assign value to x if condition is value> In your case this would mean: if true = true then x is 10, else x = 20. In your case x = always 10, because true is always true. An example that would make (a little) more sense is this one
if you compile (javac XTC20) it, you can run it like this: > java XTC20 10 output will be 10 > java XTC20 20 output will be 20 > java XTC20 30 output will be 20
conditionExpression? executeExpression1: executeExpression2 if the "conditionExpression" is true then run the "executeExpression1" else run latter. executeExpression is not only value ,it also could be function.