为什么三元运算给出空指针而它的 ifelse 对应物没有? [英] Why ternary operation gives nullpointer while its ifelse counterpart doesn't?
问题描述
我在下面的一个实例中收到 NullPointerException,而其对应的运行流畅.
I am getting NullPointerException in one instance below while its counterpart runs smooth.
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);
}
在线版本,main
中的行颠倒了,输出 null
从withIfElse
然后在 withTernary
中失败.
Online version with the lines in main
reversed, which outputs null
from withIfElse
and then fails in withTernary
.
我正在使用以下 java 版本
I am using following java version
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)
推荐答案
这是来自 规范(第 15.25.2 节):
布尔条件表达式是独立的表达式(§15.2).
Boolean conditional expressions are standalone expressions (§15.2).
布尔条件表达式的类型确定如下:
The type of a boolean conditional expression is determined as follows:
如果第二个和第三个操作数都是
Boolean
类型,则条件表达式的类型为Boolean
.
If the second and third operands are both of type
Boolean
, the conditional expression has typeBoolean
.
否则,条件表达式的类型为boolean
.
Otherwise, the conditional expression has type boolean
.
因此,整个表达式的类型被认为是boolean
,并且Boolean
值被自动拆箱,导致NullPointerException
.
Therefore, the overall expression's type is considered to be boolean
, and the Boolean
value is autounboxed, causing a NullPointerException
.
正如评论中提到的,为什么以下不会引发异常?
As mentioned in the comments, why doesn't the following raise an exception?
return val == null ? null : "true".equalsIgnoreCase(val);
好吧,规范的上述摘录仅适用于布尔条件表达式,这些表达式被指定为此处(第 15.25 节):
Well, the above excerpt from the spec specifically only applies to boolean conditional expressions, which are specified 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:
独立表单的表达式(§15.2) 类型为
boolean
或Boolean
.
带括号的 boolean
表达式 (§15.8.5).
A parenthesized boolean
expression (§15.8.5).
一个类实例创建表达式(§15.9) 用于 Boolean
类.
A class instance creation expression (§15.9) for class Boolean
.
一个方法调用表达式(§15.12) 选择最具体的方法 (§15.12.2.5) 具有返回类型 boolean
或 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.)
一个 boolean
条件表达式.
由于null
不是布尔表达式,所以整个条件表达式也不是布尔条件表达式.参考表 15.2(在同一节后面),我们可以看到这样的表达式被认为具有 Boolean
类型,因此不会发生拆箱,也不会引发异常.
Since null
is not a boolean expression, the overall conditional expression is not a boolean conditional expression. Referring to Table 15.2 (later in the same section), we can see that such an expression is considered to have a Boolean
type, so no unboxing occurs, and no exception is raised.
这篇关于为什么三元运算给出空指针而它的 ifelse 对应物没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!