Java中的条件评估过程

参见英文答案 > short-circuiting behavior of conditional OR operator(||)                                    3个
可以说我有以下条件:

if ( myList == null || myList.isEmpty() || xomeX == someY )

评估这些条件的顺序是什么?左右,右到左或每次随机?

如果第一个通过,那么其他人会被忽略?

解决方法:

除了赋值运算符=之外,它应始终从左到右.您正在使用短路OR运算符,因此如果第一个条件为真,则不会评估其余条件.

JLS 15.24

The conditional-or operator is syntactically left-associative (it groups left-to-right).

At run time, the left-hand operand expression is evaluated first; if the result has type Boolean, it is subjected to unboxing conversion (§5.1.8).

If the resulting value is true, the value of the conditional-or expression is true and the right-hand operand expression is not evaluated.

上一篇:CS224n 课程学习笔记 Lesson2


下一篇:python-使用pyparsing进行部分评估