<分区>
public static void main(String[] args) {
Object o=true?new Integer(1):new Double(1.0);
System.out.println(o);
}
我得到 1.0 作为输出,首先是上面的 else 语句无法访问,但它是如何自动类型转换的。
标签 java
<分区>
public static void main(String[] args) {
Object o=true?new Integer(1):new Double(1.0);
System.out.println(o);
}
我得到 1.0 作为输出,首先是上面的 else 语句无法访问,但它是如何自动类型转换的。
最佳答案
JLS 指出
Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules: • If either operand is of type double, the other is converted to double.
在表达式中
true? Integer(1) : Double(1.0)
因为这里的操作数之一是 Double,所以返回类型也是 double
关于java - 从 Java 程序获得意外输出时的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19311246/