为什么会抛出 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
}
?
解决办法是顺便把false
替换成Boolean.FALSE
,避免null
被拆箱成boolean
——这是不可能的。但这不是问题。问题是为什么? JLS 中是否有任何引用资料证实了这种行为,尤其是第二种情况?
最佳答案
区别在于 returnsNull()
的显式类型方法在编译时影响表达式的静态类型:
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 Conditional Operator ? : 部分
对于 E1,第 2 和第 3 个操作数的类型为
Boolean
和boolean
分别,所以这个条款适用: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.
由于表达式的类型是
boolean
,第二个操作数必须强制为boolean
.编译器将自动拆箱代码插入到第二个操作数(returnsNull()
的返回值)以使其类型为boolean
.这当然会导致来自null
的 NPE在运行时返回。对于 E2,第 2 和第 3 操作数的类型为
<special null type>
(不是 E1 中的Boolean
!)和boolean
分别,所以没有特定的类型条款适用( go read 'em! ),所以最后的“否则”条款适用: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 ==
<special null type>
(见 §4.1) - S2 ==
boolean
- T1 == 盒子(S1) ==
<special null type>
(参见 §5.1.7 中的拳击转换列表中的最后一项) - T2 == box(S2) == ` boolean 值
- lub(T1, T2) ==
Boolean
所以条件表达式的类型是
Boolean
并且第三个操作数必须强制为Boolean
.编译器为第三个操作数 (false
) 插入自动装箱代码。第二个操作数不需要自动拆箱,如E1
,因此在null
时不会自动拆箱 NPE被退回。- S1 ==
这个问题需要类似的类型分析:
关于java - boolean 值、条件运算符和自动装箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3882095/