java - boolean 值、条件运算符和自动装箱

标签 java nullpointerexception boolean conditional-operator 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
}

?

解决办法是顺便把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 个操作数的类型为 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在运行时返回。

  • 对于 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被退回。


这个问题需要类似的类型分析:

Java conditional operator ?: result type

关于java - boolean 值、条件运算符和自动装箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3882095/

相关文章:

python - Configobj - 使用 as_bool 读取值

java - phonegap中的Android后台服务插件

java - 编译单个 Java 文件

java - 什么是NullPointerException,我该如何解决?

java - 无法摆脱比较器的 NullPointerException

Java - boolean 逻辑 - 无法识别对象

java - 如何设置 ArrayList 中两个对象之间的相等比较?

Java 时区问题

java - 当CPU非常繁忙时,我的多线程java程序出现意外异常

html - 复选框检查 boolean 值是否为真与 Angular2