请问下列的输出结果为:
char ch='E';
int x =3;
System.out.println(false?102/2:'e');
System.out.println(false?x:ch);
System.out.println(true?ch:x);
System.out.println(false?3:ch);答案是:
e
69
69
E
非常郁闷 不晓得为什么是这样的

解决方案 »

  1.   


    System.out.println(false?102/2:'e'); 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.一个操作数是char,另一个是int的常量表达式,在char的范围以内,结果是char。
      

  2.   

    The type of a conditional expression is determined as follows:
    If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression. 
    If one of the second and third operands is of type boolean and the type of the other is of type Boolean, then the type of the conditional expression is boolean. 
    If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type. 
    Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases: 
    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. 
    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. 
    If one of the operands is of type Byte and the other operand is a constant expression of type int whose value is representable in type byte, then the type of the conditional expression is byte. 
    If one of the operands is of type Short and the other operand is a constant expression of type int whose value is representable in type short, then the type of the conditional expression is short. 
    If one of the operands is of type; Character and the other operand is a constant expression of type int whose value is representable in type char, then the type of the conditional expression is char. 
    Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands. Note that binary numeric promotion performs unboxing conversion (§5.1.8) and value set conversion (§5.1.13). 
    Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2. The type of the conditional expression is the result of applying capture conversion (§5.1.10) to lub(T1, T2) (§15.12.2.7). 
    参考:http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.25
      

  3.   

    System.out.println(false?x:ch); 
    System.out.println(true?ch:x); 
    因为不是常量表达式,进行binary numeric promotion。
    char->int,所以。System.out.println(false?3:ch); 同第一个输出
      

  4.   

    System.out.println(false?102/2:'e');三元运算符号: 条件?表达式1:表达式2
    条件为真 则运行表达式1 
    条件为假 则运行表达式2
      

  5.   

    其实就是判断输出的是char字符,还是转换了的数字
      

  6.   

    goldenfish1919
    谢谢你的回答 
    我也研究了这段 可是还是无法完全解决啊
    比喻System.out.println(false?32.0:ch);
    System.out.println(false?3:ch);
    输出结果为:
    69.0
    E为什么都是常量表达式
    为什么是DOUBLE的时候 还是输出为DOUBLE
      

  7.   

    char ch='E';
    int x =3;
    double y =3.0;
    System.out.println(false?3.0:ch);//1
    System.out.println(false?3:ch);//2
    System.out.println(false?y:ch);//3
    System.out.println(true?y:ch);//4
    System.out.println(true?3:ch);//5
    System.out.println(false?x:ch);//6
    运行结果是:
    69.0
    E
    69.0
    3.0
    
    69
    重点解释下
    1和2的对比
    2和6的对比
    还有5为什么是乱码
    也要注意下 1和3的结果是一样的
    哪么为什么2和6的结果就不一样了 
      

  8.   

    ASCII码中 69 对应 E   3 对应  
      

  9.   


    System.out.println(true?3:ch);//5 
    哪为什么 不直接输出3呢 还有输出的是ASCII的3呢?
      

  10.   

    1楼的就是答案。。
    输出的是3的char形,默认是ASCII
    System.out.pritn((char)3);就是这个
      

  11.   

    System.out.println(false?32.0:ch);  这里的32.0是常量表达式,但是却不是int类型,所以!
    System.out.println(false?3:ch); 3是int常量表达式,所以!
      

  12.   

    JAVA解惑里面有的
    LZ去找找这本书
      

  13.   


    谜题8:Dos Equis
    这个谜题将测试你对条件操作符的掌握程度,这个操作符有一个更广为人知的名
    字:问号冒号操作符。下面的程序将会打印出什么呢?
    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);
    }
    }
    这个程序由两个变量声明和两个print 语句构成。第一个print 语句计算条件表
    达式(true ? x : 0)并打印出结果,这个结果是char 类型变量x 的值’X’。而
    第二个print 语句计算表达式(false ? i : x)并打印出结果,这个结果还是依
    旧是’X’的x,因此这个程序应该打印XX。然而,如果你运行该程序,你就会
    发现它打印出来的是X88。这种行为看起来挺怪的。第一个print 语句打印的是
    X,而第二个打印的却是88。它们的不同行为说明了什么呢?答案就在规范有关条件表达式部分的一个阴暗的角落里。请注意在这两个表达式
    中,每一个表达式的第二个和第三个操作数的类型都不相同:x 是char 类型的,
    而0 和i 都是int 类型的。就像在谜题5 的解答中提到的,混合类型的计算会引
    起混乱,而这一点比在条件表达式中比在其它任何地方都表现得更明显。你可能
    考虑过,这个程序中两个条件表达式的结果类型是相同的,就像它们的操作数类
    型是相同的一样,尽管操作数的顺序颠倒了一下,但是实际情况并非如此。确定条件表达式结果类型的规则过于冗长和复杂,很难完全记住它们,但是其核
    心就是一下三点:
    • 如果第二个和第三个操作数具有相同的类型,那么它就是条件表达式的类
    型。换句话说,你可以通过绕过混合类型的计算来避免大麻烦。
    • 如果一个操作数的类型是T,T 表示byte、short 或char,而另一个操作
    数是一个int 类型的常量表达式,它的值是可以用类型T 表示的,那么条
    件表达式的类型就是T。
    • 否则,将对操作数类型运用二进制数字提升,而条件表达式的类型就是第
    二个和第三个操作数被提升之后的类型。2、3 两点对本谜题是关键。在程序的两个条件表达式中,一个操作数的类型是
    char,另一个的类型是int。在两个表达式中,int 操作数都是0,它可以被表
    示成一个char。然而,只有第一个表达式中的int 操作数是常量(0),而第二
    个表达式中的int 操作数是变量(i)。因此,第2 点被应用到了第一个表达式
    上,它返回的类型是char,而第3 点被应用到了第二个表达式上,其返回的类
    型是对int 和char 运用了二进制数字提升之后的类型,即int。
    条件表达式的类型将确定哪一个重载的print 方法将被调用。对第一个表达式来
    说,PrintStream.print(char)将被调用,而对第二个表达式来说,
    PrintStream.print(int)将被调用。前一个重载方法将变量x 的值作为Unicode
    字符(X)来打印,而后一个重载方法将其作为一个十进制整数(88)来打印。
    至此,谜题被解开了。
    总之,通常最好是在条件表达式中使用类型相同的第二和第三操作数。否则,你
    和你的程序的读者必须要彻底理解这些表达式行为的复杂规范。
    对语言设计者来说,也许可以设计一个牺牲掉了部分灵活性,但是增加了简洁性
    的条件操作符。例如,要求第二和第三操作数必须就有相同的类型,这看起来就
    很合理。或者,条件操作符可以被定义为对常量没有任何特殊处理。为了让这些
    选择对程序员来说更加容易接受,可以提供用来表示所有原始类型字面常量的语
    法。这也许确实是一个好注意,因为它增加了语言的一致性和完备性,同时又减
    少了对转型的需求。摘自JAVA解惑