布尔值、条件运算符和自动装箱 [英] Booleans, conditional operators and autoboxing

查看:26
本文介绍了布尔值、条件运算符和自动装箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么会抛出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 中是否有任何参考资料证实了这种行为,尤其是第二种情况?

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() 方法的显式类型会影响编译时表达式的静态类型:

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,第2个和第3个操作数的类型分别是Booleanboolean,因此该子句适用:

如果第二个和第三个操作数之一是布尔类型,另一个是布尔类型,则条件表达式的类型是布尔.

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.

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,第二个和第三个操作数的类型是 (不是 E1 中的 Boolean!)和 boolean 分别,所以没有具体的类型条款适用 (去阅读它们!),所以最后的否则"子句适用:

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 应用装箱转换后的类型.条件表达式的类型是将捕获转换(第 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 == <特殊空类型>(参见 §4.1)
  • S2 == boolean
  • T1 == box(S1) == (请参阅 §5.1.7)
  • T2 == box(S2) == `Boolean
  • lub(T1, T2) == Boolean
    • 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
    • 所以条件表达式的类型是Boolean,并且第三个操作数必须被强制转换为Boolean.编译器为第三个操作数 (false) 插入自动装箱代码.第二个操作数不需要 E1 中的自动拆箱,因此当返回 null 时没有自动拆箱 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:

      Java 条件运算符 ?: 结果类型

      这篇关于布尔值、条件运算符和自动装箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆