Java三元(即时)评估 [英] Java ternary (immediate if) evaluation

查看:97
本文介绍了Java三元(即时)评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到规范的相关部分来回答这个问题。
在Java中的条件运算符语句中,是否评估了true和false参数?

I can't find the relevant portion of the spec to answer this. In a conditional operator statement in Java, are both the true and false arguments evaluated?

以下可能会抛出NullPointerException

So could the following throw a NullPointerException

Integer test = null;

test != null ? test.intValue() : 0;


推荐答案

既然你想要规格,这里就是(来自§15.25条件运算符?:,该部分的最后一句):

Since you wanted the spec, here it is (from §15.25 Conditional Operator ? :, the last sentence of the section):


未针对条件表达式的特定评估评估未选择的操作数表达式。 / p>

The operand expression not chosen is not evaluated for that particular evaluation of the conditional expression.

这篇关于Java三元(即时)评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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