区别在于在编译时静态键入表达式:
摘要
E1: `(false ? 1.0f : null)` - arg 2 '1.0f': type float, - arg 3 'null': type null - therefore operator ?: : type Float (see explanation below) - therefore autobox arg2 - therefore autobox arg3E2: `(false ? 1.0f : (false ? 1.0f : null))` - arg 2 '1.0f' : type float - arg 3 '(false ? 1.0f : null)' : type Float (this expr is same as E1) - therefore, outer operator ?: : type float (see explanation below) - therefore un-autobox arg3
详细说明:
通过阅读规格书以及从您获得的结果开始倒推,这是我的理解。它归结为f2的第三操作数的类型
内 的条件是空型,而f2的第三操作数的类型 外 的条件被认为是浮点。
注意: 重要的是要记住,类型的确定和装箱/拆箱代码的插入是在编译时完成的。装箱/拆箱代码的实际执行在运行时完成。
Float f1 = (false ? 1.0f : null);Float f2 = (false ? 1.0f : (false ? 1.0f : null));
f1条件和f2内部条件: (false?1.0f:null)
f1条件和f2内部条件相同: (false?1.0f:null) 。f1条件和f2内部条件中的操作数类型为:
type of second operand = floattype of third operand = null type (§4.1)
§15.25中的大多数规则都已通过,并且最终评估确实适用:
否则,第二和第三操作数分别为S1和S2类型。令T1为对S1进行装箱转换所产生的类型,而T2为对S2进行装箱转换所产生的类型。条件表达式的类型是将捕获转换(第5.1.10节)应用于lub(T1,T2)(第15.12.2.7节)的结果。
S1 = floatS2 = null typeT1 = FloatT2 = null typetype of the f1 and f2 inner conditional expressions = Float
因为对于f1,赋值是给Float参考变量的,所以表达式(空)的结果已成功赋值。
对于f2外部条件:( 假?1.0f:[f2内部条件])
对于f2外部条件,类型为:
type of second operand = floattype of third operand = Float
请注意,与直接引用 空 文字的f1 /
f2内部条件相比,操作数类型有所不同(第4.1节)。由于具有2个数字可转换类型的区别,因此适用第15.12.2.7节的规则:
*否则,如果第二个和第三个操作数具有可转换为数字类型(第5.1.8节)的类型,则有以下几种情况:…
*否则,将二进制数值提升(第5.6.2节)应用于操作数类型,条件表达式的类型是第二和第三操作数的提升类型。请注意,二进制数值升级执行
拆箱转换
(第5.1.8节)和值集转换(第5.1.13节)。
由于对f2内部条件(空)的结果执行了拆箱转换,因此引发了NullPointerException。



