Do &= and |= short-circuit in Java?
Asked Answered
G

2

43

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.

Generate answered 30/6, 2010 at 18:25 Comment(0)
A
50

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

Aggri answered 30/6, 2010 at 18:26 Comment(2)
I don't think it's that inexplicable... As a matter of fact, now that I think about it they would be almost completely useless. The only value that would change after assignment is True and False. That's 1 out of 8 cases that would render the assignment portion useful. In the 7 other cases there's no need for the result because it won't change. Of course the | and & make sense because you can also use them for bitwise or/and, and hence perform some useful operations.Barbiebarbieri
I also think it's explicable. Not that I can, um, explic it, exactly, but I do tend to read assignment statements right-to-left (first do this, then do something with that result) so I'd be very surprised if the LHS magically made the RHS not execute.Rashid
P
4

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.

Pharyngitis answered 30/6, 2010 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.