Dos Equis:下列程序打印什么?char x = 'x';
int i = 0;
System.out.println(true ? x : 0);
System.out.println(false ? i : x);

解决方案 »

  1.   

    char x = 'x';
    int i = 0;
    System.out.println(true ? x : 0);
    System.out.println(false ? 0 : x);
    System.out.println(false ? i : x);这样效果也不一样,呵呵 有意思
      

  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, then the type of the conditional expression is T.
      

  3.   

    System.out.println(false ? i : x);  <-- 120
    System.out.println(false ? 0 : x);  <-- x
      

  4.   

    If one of the operands is of type byte or Byte and the other is of type short or Short, then the type of the conditional expression is short.boolean ? byte : short --> short
    boolean ? int : char --> int
    ....