public class DosEquis{
public static void main(String[] args){
char x = 'X';
int i = 0;
System.out.println(true ? x : 0);
System.out.println(false ? i : x);
}
}
Solution:
显示:X88
?: 选择操作符的特别的地方:当选择的数有int类型出现时,char类型要转化为int显示;
TID:
mixed-type computation can be confusing. Nowhere is this more apparent than in
conditional expressions.
use the same type for the second and third operands in conditional expressions.
The rules for determining the result type of a conditional expression are too long
and complex to reproduce in their entirety, but here are three key points:
1. If the second and third operands have the same type, that is the type of the
conditional expression. In other word, you can avoid the whole mess by steering clear of
mixed-type computation.
2. 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, the
type of the conditional expression is T.
3. Otherwise, binary numberic promotion is applied to the operand types, and the
type of the conditional expression is the promoted type of the second and third operands.