java - 为什么三元运算符给出空指针而它的 ifelse 运算符却没有?

标签 java nullpointerexception ternary-operator

<分区>

我在下面的一个实例中得到 NullPointerException,而它的对应实例运行平稳。

public static void main(String[] args){
    System.out.println(withTernary(null, null)); //Null Pointer
    System.out.println(withIfElse(null, null));  //No Exception
}

private static Boolean withTernary(String val, Boolean defVal){
    return val == null ? defVal : "true".equalsIgnoreCase(val);
}

private static Boolean withIfElse(String val, Boolean defVal){
    if (val == null) return defVal;
    else return "true".equalsIgnoreCase(val);
}

Online version

Online version with the lines in main reversed ,它从 withIfElse 输出 null,然后在 withTernary 中失败。

我正在使用以下 java 版本

java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

最佳答案

这是来自 the spec (§15.25.2) 的相关引述:

Boolean conditional expressions are standalone expressions (§15.2).

The type of a boolean conditional expression is determined as follows:

  • If the second and third operands are both of type Boolean, the conditional expression has type Boolean.

  • Otherwise, the conditional expression has type boolean.

因此,整个表达式的类型被认为是booleanBoolean 值被自动拆箱,导致NullPointerException


如评论中所述,为什么以下不引发异常?

return val == null ? null : "true".equalsIgnoreCase(val);

好吧,上面的规范摘录仅适用于 boolean 条件表达式,指定为here (§15.25) :

If both the second and the third operand expressions are boolean expressions, the conditional expression is a boolean conditional expression.

For the purpose of classifying a conditional, the following expressions are boolean expressions:

  • An expression of a standalone form (§15.2) that has type boolean or Boolean.

  • A parenthesized boolean expression (§15.8.5).

  • A class instance creation expression (§15.9) for class Boolean.

  • A method invocation expression (§15.12) for which the chosen most specific method (§15.12.2.5) has return type boolean or Boolean.
    (Note that, for a generic method, this is the type before instantiating the method's type arguments.)

  • A boolean conditional expression.

因为 null 不是 boolean 表达式,所以整个条件表达式不是 boolean 条件表达式。引用表 15.2(稍后在同一节中),我们可以看到这样的表达式被认为具有 Boolean 类型,因此不会发生拆箱,也不会引发异常。

关于java - 为什么三元运算符给出空指针而它的 ifelse 运算符却没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25928896/

相关文章:

Java Swing JLabel.setIcon() 没有按我期望的方式工作

java - 编辑文本字段未检测到。抛出空指针异常

javascript - 执行此操作的最快方法?三元运算符?转变?大批?

javascript - 三元运算符的单返回

java - 在 Java 中使用没有实例变量的实例方法

java - 将输入字符串转换为二维数组,然后使用索引文件将其提取

java - 使用来自 ChannelSftp.get 的输入流时 ChannelSftp.put 方法卡住

tomcat - 负载测试下 dispatcherServlet 的 Spring Boot 应用程序错误

c# - .NET开发人员如何看待条件运算符?

java - 这种分组方法有什么问题吗? (分割字符串)