public class DosEquis{
public static void main(String[] args){
int i = 0;
System.out.println(true ? 'X' : 0);
System.out.println(true ? 'X' : i);
}
}
输出为什么是
X
88

解决方案 »

  1.   

    不是为true吗? 不输出X输出什么哦
      表达式0 ? 表达式1 : 表达式2
    表示, 如果表达式0返回的boolean值是true, 则执行表达式1,否则执行表达式2 
      

  2.   

    true ? 'X' : 0
    表达式理解为char型。
    true ? 'X' : i
    这里有个int类型,表达式的类型为int型。根据表达式的类型调用println的相应的重载版本。关键:如何判断?:表达式的类型,参考语言规范。
      

  3.   

    我想是  第一个 sysout  char 与 字符串    输出  字符串的值
            第二个 sysout  char 与 int      输出了  char 的 code
      

  4.   

    true ? 'X' : 0
    0不也是int吗?
    只是没有显示的声明
    就不认为他是int吗?
      

  5.   

    0是常数,而且是char可以表示的。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.If the second and third operands have the same type, that is the type of the conditional expression. In other words, you can avoid the whole mess by steering clear of mixed-type computation.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.Otherwise, binary numeric promotion is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands.
      

  6.   

    Thank you!
    我试了一个char不能表示的数字999999,果然输出88了~