The Scrum Daily Meeting
June 6, 2011
Leave a comment
Categories: Uncategorized
daily meeting, fun, scrum
Conditional Operator ?
June 5, 2011
Leave a comment
What should be the output of this code???
public static void main(String[] args) { int i1 = 0; final int i2 = 0; System.out.println(false ? i1 : 'X'); System.out.println(false ? i2 : 'X'); }
88
X
I was initially surprised… But actually, this is how the Conditional Operator works. Here is what I read in the “The Java Language Specification 3rd edition”.
If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type int whose value is representable in type T, then the type of the conditional expression is T.
The Conditional Operator uses the second and the third operand to determine the type of the resulting value.
The complete specification can be found here.
Categories: Uncategorized
conditional, java, operator, ternary