什么是短路以及在 Java 编程中如何使用它?
可能的重复:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
短路是指一旦确定了表达式的结果就停止对其求值。例如:
如果
a == b
为 true,则c == d
和e == f
永远不会在以下位置求值全部,因为表达式的结果已经确定。如果a == b
为 false,则计算c == d
;如果为 true,则永远不会计算e == f
。这似乎没有任何区别,但请考虑:如果
foo()
返回 true,则bar
和baz
永远不会被调用,因为表达式的结果已经确定。因此,如果bar
或baz
除了返回某些内容之外还有其他效果(副作用),这些效果永远不会发生。短路的一个很好的例子与对象引用有关:
如果
a
为null,
,但由于表达式短路,如果a.getFoo()
通常会抛出NullPointerException
a != null
为false
,则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
is true, thenc == d
ande == f
are never evaluated at all, because the expression's outcome has already been determined. ifa == b
is false, thenc == d
is evaluated; if it's true, thene == f
is never evaluated. This may not seem to make any difference, but consider:If
foo()
returns true, thenbar
andbaz
are never called, because the expression's outcome has already been determined. So ifbar
orbaz
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:
a.getFoo()
would normally throw aNullPointerException
ifa
werenull
, but because the expression short-circuits, ifa != null
isfalse
, thea.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.短路求值表示在求值布尔表达式时(逻辑
AND 和
OR
),一旦找到满足或否定表达式的第一个条件,就可以停止。例如,假设您正在使用多个子表达式评估逻辑
OR
,其中每个子表达式的评估成本都非常高:一旦发现返回值,JVM 就会停止评估“costlyTest”函数
true
,因为这将立即满足布尔表达式。因此,如果 costlyTest1() 返回 true,则将跳过其他测试。类似地:一旦 JVM 发现返回 false 的函数,它就可以停止计算函数,因为这会立即否定表达式;因此,如果 costlyTest1() 返回 false,则不会调用其他函数。
这些规则适用于布尔表达式的任何级别的嵌套,并且可以利用这些规则来避免不必要的工作,如上面的示例所示。
Short-circuit evaluation means that when evaluating boolean expressions (logical
AND
andOR
) 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: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 ifcostlyTest1()
returns true then the other tests will be skipped. Similarly:The JVM can stop evaluating the functions as soon as it finds one that returns
false
, since that immediately negates the expression; so ifcostlyTest1()
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.
短路
:如果第一部分为true
,则不必费心评估表达式的其余部分。同样的逻辑适用于&&
情况下的false
,这也是短路Short Circuit
: If the first part istrue
don't bother evaluating the rest of the expression. Same logic applies forfalse
in the case of&&
which is also short circuiting短路表达式的计算意味着在找到其值之前只需要计算表达式的一部分。例如:
如果
a
为null
,则不会计算a.size() == 0
子表达式,因为布尔运算符 <如果其操作数之一为true
,则 code>|| 的计算结果为true
。类似地,对于此表达式:
如果
a
为null
,则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:
If
a
isnull
, thea.size() == 0
subexpression won't be evaluated, because the boolean operator||
evaluates totrue
if one of its operands istrue
.Similarly, for this expression:
If
a
isnull
, then thea.size() > 0
won't be evaluated, because the boolean operator&&
evaluates tofalse
if one of its operands isfalse
.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.短路是使用逻辑 AND 或 OR 运算符(& 或 |)的另一种方法,
例如非短路 OR
即使 false 不是 true(事实总是如此),第一个条件和第二个条件都会被评估。
然而,它被写成短路 OR:
第一个条件仅被评估,因为它是 false,true 不会被评估,因为它不是必需的。
Short circuiting is an alternative way of using the logical AND or OR operators (& or |)
e.g. a non short-circuit OR
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:
The first condition is only evaluated since it's false, true isn't evaluated since it's not required.