在布尔结果已知后,Java 是否评估剩余条件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6352139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:30:54  来源:igfitidea点击:

Does Java evaluate remaining conditions after boolean result is known?

java

提问by P. Deters

That is, if I have a statement that evaluates multiple conditions, in say a 'or' statement like so..

也就是说,如果我有一个评估多个条件的语句,比如像这样的“或”语句..

if(isVeryLikely() || isSomewhatLikely() || isHardlyLikely())
{
    ...
}

In the case that isVeryLikely()returns true at runtime, will isSomewhatLikely()and isHardlyLikely()execute? How about if instead of methods they were static booleans?

isVeryLikely()运行时返回true的情况下,will isSomewhatLikely()and isHardlyLikely()execute?如果它们是静态布尔值而不是方法呢?

回答by Chris Jester-Young

The ||and &&operators are short-circuiting.

||&&运营商短路。

true || willNeverExecute();
false && willNeverExecute();

回答by Vern

The first thing that you need to note is that Java conditional statements can only take boolean, unlike other languages like C/C++ where any non-zero value will evaluate to true.

您需要注意的第一件事是,Java 条件语句只能采用布尔值,这与 C/C++ 等其他语言不同,任何非零值都将评估为真。

That being said, there are 2 types of operators, the first is known as the shor-circuit types:

也就是说,有两种类型的运算符,第一种称为短路类型:

&& and ||

while the other are the NON-short-circuit types:

而另一种是非短路类型:

& and |

For the Short-Circuit types, once a logical output can be found as an answer to the expression, the rest of the statement will be dumped. For the NON-Short-Circuit types, they will continue to evaluate all conditions.

对于短路类型,一旦可以找到逻辑输出作为表达式的答案,语句的其余部分将被转储。对于非短路类型,他们将继续评估所有条件。

With this in mind, what you have:

考虑到这一点,你有什么:

if(isVeryLikely() || isSomewhatLikely() || isHardlyLikely())
{
    ...
}

Java will:

Java 将:

  1. First check if isVeryLikely() returns true. If true, then it will not continue further.
  2. If isVeryLikely() returns false, then invoke isSomewhatLikely() to see if it returns true. If true, nothing else if evaluated. If false, goto 3.
  3. isHardlyLikely() is invoked. If true, entire expression is true, otherwise false, entire expression is false.
  1. 首先检查 isVeryLikely() 是否返回 true。如果为真,则不会再继续下去。
  2. 如果 isVeryLikely() 返回 false,则调用 isSomewhatLikely() 以查看它是否返回 true。如果为真,则不进行其他评估。如果为假,请转到 3。
  3. isHardlyLikely() 被调用。如果为真,则整个表达式为真,否则为假,整个表达式为假。

The entire concept you're asking about is "How does Java evaluate Logical Expressions." Good question :)

您要问的整个概念是“Java 如何评估逻辑表达式”。好问题 :)

Hope it helps! Cheers!

希望能帮助到你!干杯!

回答by highlycaffeinated

No, java uses short-circuit evaluation on expressions using ||and &&. See herefor more info.

不,java 对使用||和的表达式使用短路评估&&。请参阅此处了解更多信息。

回答by Maciej

Because the || is short circuiting, the statement will be evaluated to true as soon as it hits the first true condition, regardless of whether or not the operands are static.

因为 || 是短路,一旦遇到第一个真条件,该语句将被评估为真,无论操作数是否是静态的。

In short, no the two other functions will not be evaluated if the first returns true.

简而言之,如果第一个返回 true,则不会评估其他两个函数。

回答by ergonaut

The short answer is, it will evaluate until has enough to conclude whether it is T/F.

简短的回答是,它会一直评估直到有足够的东西来断定它是否是 T/F。

There is something called boolean short-circuiting. Essentially it will try and evaluate only what it needs to (if you use the && or || operators) and the leave. You can take advantage of this in a few ways:

有一种叫做布尔短路的东西。本质上,它只会尝试和评估它需要的东西(如果你使用 && 或 || 运算符)和离开。您可以通过以下几种方式利用这一点:

(a() || b())
  1. If b() would throw an exception, if a() is true, then it would not even try and check the b(). A type of chain-checking mechanism.

  2. If the latter evaluations are resource consuming, you can move them to the end of the evaluation (eg. b() takes 10 times longer)

  3. If the most likely path can be determined by a certain clause, put them first. This would also speed up execution.

  1. 如果 b() 会抛出异常,如果 a() 为真,那么它甚至不会尝试检查 b()。一种链式检查机制。

  2. 如果后面的评估是资源消耗,您可以将它们移动到评估的末尾(例如,b() 需要 10 倍的时间)

  3. 如果某个子句可以确定最可能的路径,则将它们放在首位。这也将加快执行速度。

回答by LPV

As an update for Kotlin users, you can use 'or' operator in Kotlin std library in order to check all the expressions in the if statement and not performing a short-circuit.

作为 Kotlin 用户的更新,您可以在 Kotlin 标准库中使用“或”运算符来检查 if 语句中的所有表达式并且不执行短路。

In the expression presented in the initial question the code in Kotlin should be:

在最初问题中提出的表达式中,Kotlin 中的代码应该是:

if(isVeryLikely() or isSomewhatLikely() or isHardlyLikely()) {
    ...
}

回答by Dhruv Rai Puri

Whether for boolean expression specified in the questions are static booleans, or they are methods returning a boolean value, in both the cases - expression evaluation will start from the left and conditions will be evaluated one-by-one. The first condition which gives a value true will short-circuit the evaluation. It will directly give the result as true with the remaining conditions not being evaluated. This is how ||(OR) works in Java using the concept of short-circuit evaluation.

问题中指定的布尔表达式是静态布尔值,还是返回布尔值的方法,在这两种情况下 - 表达式评估将从左侧开始,条件将逐一评估。给出值 true 的第一个条件将使评估短路。它将直接给出结果为真,其余条件不进行评估。这就是 ||(OR) 在 Java 中使用短路评估概念的工作方式。

If you want to understand more about short-circuits in general you can refer an article I have written on my blog - http://www.javabrahman.com/programming-principles/short-circuiting-or-short-circuits-in-boolean-evaluations-in-programming-and-java/

如果你想了解更多关于短路的一般知识,你可以参考我在我的博客上写的一篇文章 - http://www.javabrahman.com/programming-principles/short-circuiting-or-short-circuits-in- boolean-evaluations-in-programming-and-java/