什么是短路以及在 Java 编程中如何使用它?

发布于 2025-01-07 11:06:24 字数 499 浏览 0 评论 0原文

可能的重复:
java 是否在已知布尔结果后评估剩余条件
为什么我们通常使用|| 不是 |,有什么区别?

前几天我错过了课堂讲座,我想知道是否有人可以解释什么是短路,也许可以举一个例子在简单的 Java 程序中使用。感谢您的帮助!

Possible Duplicate:
Does java evaluate remaining conditions after boolean result is known
Why do we usually use || not |, what is the difference?

I missed my class lecture the other day and I was wondering if anyone could give an explanation what short circuiting is and maybe an example of it being used in a simple Java program. Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤独岁月 2025-01-14 11:06:24

短路是指一旦确定了表达式的结果就停止对其求值。例如:

if (a == b || c == d || e == f) {
    // Do something
}

如果 a == b 为 true,则 c == de == f 永远不会在以下位置求值全部,因为表达式的结果已经确定。如果 a == b 为 false,则计算 c == d;如果 true,则永远不会计算e == f。这似乎没有任何区别,但请考虑:

if (foo() || bar() || baz()) {
    // Do something
}

如果 foo() 返回 true,则 barbaz 永远不会被调用,因为表达式的结果已经确定。因此,如果 barbaz 除了返回某些内容之外还有其他效果(副作用),这些效果永远不会发生。

短路的一个很好的例子与对象引用有关:

if (a != null && a.getFoo() != 42) {
    // Do something
}

如果 anull,a.getFoo() 通常会抛出 NullPointerException,但由于表达式短路,如果 a != nullfalse,则 a.getFoo() 部分永远不会发生了,所以我们没有例外。

请注意,并非所有表达式都是短路的。 ||&& 运算符是短路的,但 |& 不是, */ 也不是;事实上大多数运营商都不是。

Short-circuiting is where an expression is stopped being evaluated as soon as its outcome is determined. So for instance:

if (a == b || c == d || e == f) {
    // Do something
}

If a == b is true, then c == d and e == f are never evaluated at all, because the expression's outcome has already been determined. if a == b is false, then c == d is evaluated; if it's true, then e == f is never evaluated. This may not seem to make any difference, but consider:

if (foo() || bar() || baz()) {
    // Do something
}

If foo() returns true, then bar and baz are never called, because the expression's outcome has already been determined. So if bar or baz has some other effect than just returning something (a side effect), those effects never occur.

One great example of short-circuiting relates to object references:

if (a != null && a.getFoo() != 42) {
    // Do something
}

a.getFoo() would normally throw a NullPointerException if a were null, but because the expression short-circuits, if a != null is false, the a.getFoo() part never happens, so we don't get an exception.

Note that not all expressions are short-circuited. The || and && operators are short-circuited, but | and & are not, nor are * or /; in fact most operators are not.

第七度阳光i 2025-01-14 11:06:24

短路求值表示在求值布尔表达式时(逻辑AND 和 OR),一旦找到满足或否定表达式的第一个条件,就可以停止。

例如,假设您正在使用多个子表达式评估逻辑 OR,其中每个子表达式的评估成本都非常高:

if (costlyTest1() || costlyTest2() || costlyTest3()) { // ...

一旦发现返回值,JVM 就会停止评估“costlyTest”函数true,因为这将立即满足布尔表达式。因此,如果 costlyTest1() 返回 true,则将跳过其他测试。类似地:

if (costlyTest1() && costlyTest2() && costlyTest3()) { // ...

一旦 JVM 发现返回 false 的函数,它就可以停止计算函数,因为这会立即否定表达式;因此,如果 costlyTest1() 返回 false,则不会调用其他函数。

这些规则适用于布尔表达式的任何级别的嵌套,并且可以利用这些规则来避免不必要的工作,如上面的示例所示。

Short-circuit evaluation means that when evaluating boolean expressions (logical AND and OR) you can stop as soon as you find the first condition which satisfies or negates the expression.

For example, suppose you were evaluating a logical OR with several sub-expressions, each of which is very costly to evaluate:

if (costlyTest1() || costlyTest2() || costlyTest3()) { // ...

The JVM can stop evaluating the "costlyTest" functions as soon as it finds one that returns true, since that will immediately satisfy the boolean expression. So if costlyTest1() returns true then the other tests will be skipped. Similarly:

if (costlyTest1() && costlyTest2() && costlyTest3()) { // ...

The JVM can stop evaluating the functions as soon as it finds one that returns false, since that immediately negates the expression; so if costlyTest1() returns false then the other functions will not be called.

These rules pertain with any level of nesting of boolean expressions and can be taken advantage of to avoid unnecessary work, as demonstrated in the examples above.

绝情姑娘 2025-01-14 11:06:24

短路:如果第一部分为true,则不必费心评估表达式的其余部分。同样的逻辑适用于 && 情况下的 false,这也是短路

Short Circuit: If the first part is true don't bother evaluating the rest of the expression. Same logic applies for false in the case of && which is also short circuiting

メ斷腸人バ 2025-01-14 11:06:24

短路表达式的计算意味着在找到其值之前只需要计算表达式的一部分。例如:

a == null || a.size() == 0

如果 anull,则不会计算 a.size() == 0 子表达式,因为布尔运算符 <如果其操作数之一为 true,则 code>|| 的计算结果为 true

类似地,对于此表达式:

a != null && a.size() > 0

如果 anull,则 a.size() > 0 不会被计算,因为如果布尔运算符 && 的操作数之一为 false,则其计算结果为 false

在上面的示例中,布尔运算符 &&|| 被认为是短路的,因为如果值第一个操作数的值足以确定整个表达式的值。为了进行比较,&| 操作数是等效的非短路布尔运算符。

Short-circuiting the evaluation of an expression means that only a part of the expression needs to be evaluated before finding its value. For example:

a == null || a.size() == 0

If a is null, the a.size() == 0 subexpression won't be evaluated, because the boolean operator || evaluates to true if one of its operands is true.

Similarly, for this expression:

a != null && a.size() > 0

If a is null, then the a.size() > 0 won't be evaluated, because the boolean operator && evaluates to false if one of its operands is false.

In the above examples the boolean operators && and || are said to be short-circuit, given the fact that the second operand might not be evaluated if the value of the first operand is enough to determine the value of the whole expression. For comparison, the & and | operands are the equivalent non-short-circuit boolean operators.

半枫 2025-01-14 11:06:24

短路是使用逻辑 AND 或 OR 运算符(& 或 |)的另一种方法,

例如非短路 OR

if(false | true) {

}

即使 false 不是 true(事实总是如此),第一个条件和第二个条件都会被评估。

然而,它被写成短路 OR:

if(false || true) {

}

第一个条件仅被评估,因为它是 false,true 不会被评估,因为它不是必需的。

Short circuiting is an alternative way of using the logical AND or OR operators (& or |)

e.g. a non short-circuit OR

if(false | true) {

}

The first condition and second condition are both evaluated even if false is not true (which it always is).

However it is was written as a short-circuit OR:

if(false || true) {

}

The first condition is only evaluated since it's false, true isn't evaluated since it's not required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文