Java中&=和|=短路吗?

发布于 2024-09-08 02:20:26 字数 206 浏览 4 评论 0原文

换句话说,以下两个语句的行为是否相同?

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 技术交流群。

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

发布评论

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

评论(2

变身佩奇 2024-09-15 02:20:26

不,|=&= 不会短路,因为它们是 &|,不短路。

JLS 15.26.2 复合赋值运算符< /a>

E1 op= E2 形式的复合赋值表达式相当于 E1 = (T)((E1) op (E2)),其中 TE1 的类型,只不过 E1 仅计算一次。

因此,假设 boolean &isFoobared &= methodWithSideEffects() 的等价物是:

isFoobared = isFoobared & methodWithSideEffects(); // no shortcircuit

另一方面 && 和 < code>|| 做了短路,但令人费解的是,Java 没有它们的复合赋值版本。也就是说,Java 既没有 &&= 也没有 ||=

另请参阅


这个短路业务到底是什么?

boolean 逻辑运算符(&|)与其对应的 boolean 条件运算符(>&&||) 是前者不会“短路”;后者确实如此。也就是说,假设没有例外等:

  • &| 总是 计算两个操作数
  • && 和 < code>|| 有条件地计算右操作数;仅当右操作数的值可能影响二元运算的结果时,才会计算右操作数。这意味着在以下情况下不会评估正确的操作数:
    • && 的左操作数的计算结果为 false
      • (因为无论正确的操作数计算结果是什么,整个表达式都是 false
    • || 的左操作数计算结果为 true
      • (因为无论正确的操作数计算结果是什么,整个表达式都是true

参考文献

No, |= and &= do not shortcircuit, because they are the compound assignment version of & and |, which do not shortcircuit.

JLS 15.26.2 Compound Assignment Operators

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

Thus, assuming boolean &, the equivalence for isFoobared &= methodWithSideEffects() is:

isFoobared = isFoobared & methodWithSideEffects(); // no shortcircuit

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 their boolean 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:
    • The left operand of && evaluates to false
      • (because no matter what the right operand evaluates to, the entire expression is false)
    • The left operand of || evaluates to true
      • (because no matter what the right operand evaluates to, the entire expression is true)

References

半葬歌 2024-09-15 02:20:26

不,他们没有,因为 x &= yx = x & 的缩写。 yx |= yx = x | 的缩写。 y 。 Java 没有 &&=||= 运算符来执行您想要的操作。

&| 运算符(以及 ~^<<、>>>>) 是 按位运算符。表达式x & y 对于任何整型类型,都会执行按位与运算。类似地,| 执行按位或。要执行按位运算,数字中的每一位都被视为布尔值,1 表示 true0 表示 false< /代码>。因此,3 & 2 == 2,因为 3 在二进制中是 0...011,而 20... 010。类似地,3 | 2 == 3 。 Wikipedia 对不同的运算符有很好的完整解释。现在,对于布尔值,我认为您可以使用 &| 作为非短路 em> 相当于 &&||,但我无法想象你为什么想要这样做。

No, they do not, because x &= y is short for x = x & y and x |= y is short for x = x | y. Java has no &&= or ||= operators which would do what you want.

The & and | operators (along with ~, ^, <<, >>, and >>>) are the bitwise operators. The expression x & 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, with 1 indicating true and 0 indicating false. Thus, 3 & 2 == 2, since 3 is 0...011 in binary and 2 is 0...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.

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