布尔值,有条件的经营者和自动装箱 [英] Booleans, conditional operators and autoboxing
问题描述
为什么这掷 NullPointerException异常
public static void main(String[] args) throws Exception {
Boolean b = true ? returnsNull() : false; // NPE on this line.
System.out.println(b);
}
public static Boolean returnsNull() {
return null;
}
虽然这并不
public static void main(String[] args) throws Exception {
Boolean b = true ? null : false;
System.out.println(b); // null
}
解决方案是由方法来替换假
按 Boolean.FALSE
来避免空
被拆箱到布尔
可呈现是不可能的。但是,这不是问题。现在的问题是的为什么的?是否有JLS这证实了这种行为,任何引用特别的第二种情况?
The solution is by the way to replace false
by Boolean.FALSE
to avoid null
being unboxed to boolean
--which isn't possible. But that isn't the question. The question is why? Are there any references in JLS which confirms this behaviour, especially of the 2nd case?
推荐答案
的区别在于,显式类型的 returnsNull()
法的影响静态类型在编译时前pressions的:
The difference is that the explicit type of the returnsNull()
method affects the static typing of the expressions at compile time:
E1: `true ? returnsNull() : false` - boolean (auto-unboxing 2nd operand to boolean)
E2: `true ? null : false` - Boolean (autoboxing of 3rd operand to Boolean)
请参阅Java语言规范,部分 15.25条件操作?
See Java Language Specification, section 15.25 Conditional Operator ? :
-
有关E1,第二的类型和第3操作数
布尔
和布尔
分别,所以该条款适用:
For E1, the types of the 2nd and 3rd operands are
Boolean
andboolean
respectively, so this clause applies:
如果第二和第三个操作数中的一个的类型是布尔值和其他的类型是类型布尔,那么条件前pression的类型是布尔
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.
由于前pression的类型布尔
,第二个操作数必须被强制转换为布尔
。编译器插入自动拆箱code到第二个操作数( returnsNull的返回值()
),使其输入布尔
。当然,这导致从在运行时的空
返回的NPE。
Since the type of the expression is boolean
, the 2nd operand must be coerced to boolean
. The compiler inserts auto-unboxing code to the 2nd operand (return value of returnsNull()
) to make it type boolean
. This of course causes the NPE from the null
returned at run-time.
有关E2,类型2日和3操作数是<专用null类型>
(不是布尔
在E1!)和布尔
分别,所以没有具体的打字条款适用(的再去读'时间),所以最终的!,否则条款适用:
For E2, types of the 2nd and 3rd operands are <special null type>
(not Boolean
as in E1!) and boolean
respectively, so no specific typing clause applies (go read 'em!), so the final "otherwise" clause applies:
否则,第二个和第三个操作数分别是类型S1和S2。让T1是从施加装箱转换到S1产生的类型,并让T2是从施加装箱转换到S2而产生的类型。类型有条件恩pression的是应用capture转换(§5.1.10)到LUB(T1,T2)的结果(§15.12.2.7)。
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).
- S1 ==
&LT;专用null类型&GT;
(见的§4.1) - S2 ==
布尔
- T1 ==盒(S1)==
&LT;专用null类型&GT;
(见的§5.1.7) - T2 ==盒(S2)=='布尔
- LUB(T1,T2)==
布尔
- S1 ==
<special null type>
(see §4.1) - S2 ==
boolean
- T1 == box(S1) ==
<special null type>
(see last item in list of boxing conversions in §5.1.7) - T2 == box(S2) == `Boolean
- lub(T1, T2) ==
Boolean
所以,有条件的前pression的类型是布尔
和第三个操作数必须被强制转换为布尔
。编译器插入了3操作数自动装箱code(假
)。第二个操作数不需要自动拆箱在 E1
,所以没有自动拆箱NPE时空
是回来了。
So the type of the conditional expression is Boolean
and the 3rd operand must be coerced to Boolean
. The compiler inserts auto-boxing code for the 3rd operand (false
). The 2nd operand doesn't need the auto-unboxing as in E1
, so no auto-unboxing NPE when null
is returned.
这问题需要一个类似类型的分析:
This question needs a similar type analysis:
这篇关于布尔值,有条件的经营者和自动装箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!