public class Conditional {
public static void main (String [] args) {
int x = 4;
System.out.println("value is " + ((x > 4)? 99.9 : 9));
}
}
结果通过编译输出的是 9.0 为什么不是 9 ?
public static void main (String [] args) {
int x = 4;
System.out.println("value is " + ((x > 4)? 99.9 : 9));
}
}
结果通过编译输出的是 9.0 为什么不是 9 ?
• 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).
心就是一下三点:
• 如果第二个和第三个操作数具有相同的类型,那么它就是条件表达式的类
型。换句话说,你可以通过绕过混合类型的计算来避免大麻烦。
• 如果一个操作数的类型是T,T 表示byte、short 或char,而另一个操作
数是一个int 类型的常量表达式,它的值是可以用类型T 表示的,那么条
件表达式的类型就是T。
• 否则,将对操作数类型运用二进制数字提升,而条件表达式的类型就是第
二个和第三个操作数被提升之后的类型。
public static void main(String[] args){
char x = 'X';
int i = 0;
System.out.println(true ? x : 0);
System.out.println(false ? i : x);
}
楼主可以下载 java解惑 来看
关于这个问题上面解释的比较清楚
public class Conditional {
public static void main (String [] args) {
int x = 4;
System.out.println("value is " + ((x > 4)? 99 : 9.9));
}
}
【/code】这个是试着发帖格式
public class Conditional {
public static void main (String [] args) {
char x = 'X';
int i = 0;
System.out.println(true ? x : 0);
System.out.println(false ? i : x);
}
}
[/code]