使用条件表达式提升数值类型
Numeric Type Promotion with Conditional Expression
我在摆弄 java,我注意到了一些东西。它可以在这里得到最好的展示:
boolean boo = true;
Object object1 = boo ? new Integer(1) : new Double(2.0);
Object object2;
if (boo)
object2 = new Integer(1);
else
object2 = new Double(2.0);
System.out.println(object1);
System.out.println(object2);
我希望两者是一样的,但这是打印出来的:
1.0
1
有人对此有好的解释吗?
JLSsection 15.25 has a table that summarizes the type of the conditional expression based on the type of its operands. For the case of Integer
and Double
, the table says that the type will be the result of applying binary numeric promotion to the arguments (§15.25.2)
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 value set conversion (§5.1.13) and may perform unboxing conversion (§5.1.8).
If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).
...
If either operand is of type double, the other is converted to double.
这就是
正在发生的事情
Object object1 = boo ? new Integer(1) : new Double(2.0);
- 引用类型
new Integer(1)
被拆箱为原始类型 int
1.
- 引用类型
new Double(2.0)
被拆箱为原始 double
2.0.
- 执行二进制数值提升,结果类型为
double
。在这种情况下,由于 boo
是 true
,原语 int
1 将被提升为 double
作为 1.0.
- 由于您将结果存储在
Object
中,原始结果被装箱到其包装器类型中。
对于
的情况
Object object2;
if (boo)
object2 = new Integer(1);
else
object2 = new Double(2.0);
if/else 构造不执行数值提升。实际上不会有任何拳击转换。由于 boo
是 true
,因此 if
部分将被执行并且 object2
将具有值 new Integer(1)
.
三进制必须 return 两个条件的类型相同,因此您的第一个结果 (Integer
) 提升 为双精度以匹配 2.0
。另见,
Object object1 = boo ? new Integer(1) : new Double(2.0);
System.out.println(object1.getClass().getName());
这在 JLS-15.25.2 - Numeric Conditional Expressions 中记录(部分)
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 value set conversion (§5.1.13) and may perform unboxing conversion (§5.1.8).
我在摆弄 java,我注意到了一些东西。它可以在这里得到最好的展示:
boolean boo = true;
Object object1 = boo ? new Integer(1) : new Double(2.0);
Object object2;
if (boo)
object2 = new Integer(1);
else
object2 = new Double(2.0);
System.out.println(object1);
System.out.println(object2);
我希望两者是一样的,但这是打印出来的:
1.0
1
有人对此有好的解释吗?
JLSsection 15.25 has a table that summarizes the type of the conditional expression based on the type of its operands. For the case of Integer
and Double
, the table says that the type will be the result of applying binary numeric promotion to the arguments (§15.25.2)
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 value set conversion (§5.1.13) and may perform unboxing conversion (§5.1.8).
If any operand is of a reference type, it is subjected to unboxing conversion (§5.1.8).
...
If either operand is of type double, the other is converted to double.
这就是
正在发生的事情Object object1 = boo ? new Integer(1) : new Double(2.0);
- 引用类型
new Integer(1)
被拆箱为原始类型int
1. - 引用类型
new Double(2.0)
被拆箱为原始double
2.0. - 执行二进制数值提升,结果类型为
double
。在这种情况下,由于boo
是true
,原语int
1 将被提升为double
作为 1.0. - 由于您将结果存储在
Object
中,原始结果被装箱到其包装器类型中。
对于
的情况Object object2;
if (boo)
object2 = new Integer(1);
else
object2 = new Double(2.0);
if/else 构造不执行数值提升。实际上不会有任何拳击转换。由于 boo
是 true
,因此 if
部分将被执行并且 object2
将具有值 new Integer(1)
.
三进制必须 return 两个条件的类型相同,因此您的第一个结果 (Integer
) 提升 为双精度以匹配 2.0
。另见,
Object object1 = boo ? new Integer(1) : new Double(2.0);
System.out.println(object1.getClass().getName());
这在 JLS-15.25.2 - Numeric Conditional Expressions 中记录(部分)
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 value set conversion (§5.1.13) and may perform unboxing conversion (§5.1.8).