Java中&=和|=短路吗?
换句话说,以下两个语句的行为是否相同?
isFoobared = isFoobared && methodWithSideEffects();
isFoobared &= methodWithSideEffects();
我意识到我可以编写一个测试,但有人可能会立即知道这一点,而其他人可能会发现答案有用。
In other words, do the following two statements behave the same way?
isFoobared = isFoobared && methodWithSideEffects();
isFoobared &= methodWithSideEffects();
I realize I could just write up a test, but someone might know this offhand, and others might find the answer useful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,
|=
和&=
不会短路,因为它们是&
和|,不短路。
因此,假设
boolean &
,isFoobared &= methodWithSideEffects()
的等价物是:另一方面
&&
和 < code>|| 做了短路,但令人费解的是,Java 没有它们的复合赋值版本。也就是说,Java 既没有&&=
也没有||=
。另请参阅
这个短路业务到底是什么?
boolean
逻辑运算符(&
和|
)与其对应的boolean
条件运算符(>&&
和||
) 是前者不会“短路”;后者确实如此。也就是说,假设没有例外等:&
和|
总是 计算两个操作数&&
和 < code>|| 有条件地计算右操作数;仅当右操作数的值可能影响二元运算的结果时,才会计算右操作数。这意味着在以下情况下不会评估正确的操作数:&&
的左操作数的计算结果为false
false
)||
的左操作数计算结果为true
true
)参考文献
&
、^
和|
< /a>& ;&
||
No,
|=
and&=
do not shortcircuit, because they are the compound assignment version of&
and|
, which do not shortcircuit.Thus, assuming
boolean &
, the equivalence forisFoobared &= methodWithSideEffects()
is:On the other hand
&&
and||
do shortcircuit, but inexplicably Java does not have compound assignment version for them. That is, Java has neither&&=
nor||=
.See also
What is this shortcircuiting business anyway?
The difference between the
boolean
logical operators (&
and|
) compared to theirboolean
conditional counterparts (&&
and||
) is that the former do not "shortcircuit"; the latter do. That is, assuming no exception etc:&
and|
always evaluate both operands&&
and||
evaluate the right operand conditionally; the right operand is evaluated only if its value could affect the result of the binary operation. That means that the right operand is NOT evaluated when:&&
evaluates tofalse
false
)||
evaluates totrue
true
)References
&
,^
, and|
&&
||
不,他们没有,因为
x &= y
是x = x & 的缩写。 y
和x |= y
是x = x | 的缩写。 y 。 Java 没有
&&=
或||=
运算符来执行您想要的操作。&
和|
运算符(以及~
、^
、<<、
>
和>>>
) 是 按位运算符。表达式x & y
对于任何整型类型,都会执行按位与运算。类似地,|
执行按位或。要执行按位运算,数字中的每一位都被视为布尔值,1
表示true
,0
表示false< /代码>。因此,
3 & 2 == 2
,因为3
在二进制中是0...011
,而2
是0... 010
。类似地,3 | 2 == 3 。 Wikipedia 对不同的运算符有很好的完整解释。现在,对于布尔值,我认为您可以使用
&
和|
作为非短路 em> 相当于&&
和||
,但我无法想象你为什么想要这样做。No, they do not, because
x &= y
is short forx = x & y
andx |= y
is short forx = x | y
. Java has no&&=
or||=
operators which would do what you want.The
&
and|
operators (along with~
,^
,<<
,>>
, and>>>
) are the bitwise operators. The expressionx & y
will, for any integral type, perform a bitwise and operation. Similarly,|
performs a bitwise or. To perform a bitwise operation, each bit in the number is treated like a boolean, with1
indicatingtrue
and0
indicatingfalse
. Thus,3 & 2 == 2
, since3
is0...011
in binary and2
is0...010
. Similarly,3 | 2 == 3
. Wikipedia has a good complete explanation of the different operators. Now, for a boolean, I think you can get away with using&
and|
as non-short-circuiting equivalents of&&
and||
, but I can't imagine why you'd want to anyway.