Java中的条件评估过程 [英] Condition evaluation process in Java

查看:143
本文介绍了Java中的条件评估过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下条件:

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

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

What is the order of the evaluation of these conditions? Left or right, right to left or random each time?

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

If the first one passes, then the others are ignored?

推荐答案

除了赋值运算符 = 从左到右 c $ c>。您正在使用短路 OR 运算符,因此如果第一个条件是 true ,则不会对其余条件进行评估。

It should be always be left to right except the assignment operator = . You are using short circuit OR operator , hence if the first condition is true , rest of them won't be evaluated.

JLS 15.24


条件或运算符在语法上是左关联的(它左边是组) -to-right)。

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

在运行时,首先评估左侧操作数表达式;如果结果具有布尔类型,则进行拆箱转换(第5.1.8节)。

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).

如果结果值为true,则条件或表达式的值为true且未评估右侧操作数表达式。

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

这篇关于Java中的条件评估过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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