public class ou {
 public static void main(String[] args) {
 char a = 'a';
 int  b = 8;

 System.out.println(false?a:b);
 System.out.println(false?a:8);
 System.out.println(false?8:a);
 System.out.println(false?b:'a');
  
/*  打印出来是  
 8
 
 a
 97
                 请解释一下
 */
}
}

解决方案 »

  1.   

    布尔表达式?a:b
    布尔表达式为true结果为a,表达式为false结果为b
      

  2.   

    System.out.println(false?a:b); //打印b,b是8,所以打印出8
    System.out.println(false?a:8); //打印8,但a是char型的,所以打印8的char字符,也就是backspace
    System.out.println(false?8:a); //打印int型,也就是a
    System.out.println(false?b:'a'); //打印a的ASCII码
      

  3.   

     false?a:b
    有字符强转的功能.把后面的强转我和前面的类型一样
      

  4.   

    如果前面是int型 会把后面字符变成对应的ascii码。
    如果前面是char型  会把后面数字转换成对应的字符。
      

  5.   

    条件?a:b;等同于
    if(条件)
    {
       a;
    }else
    {
       b;
    }
      

  6.   

    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
      

  7.   


    谜题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解惑
      

  8.   

    三元运算符就是让你少些if else ,节省代码。
      

  9.   

    这个在java解惑里面有:
    记住三条规则:
    1.如果第二和第三个操作符的类型一样,那么结果的类型也是他们的类型
    2.如果第二个操作数的操作类型为T,T表示short,byte,char,那么另一个操作数是int类型常量表达式,他的值可以用T来表示,那么条件表达式的类型就是T
    3.否则,将对操作数的类型进行提升,结果为提升之后的值。
      

  10.   

    ][/color]