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.
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
booleanlogical operators (&and|) compared to theirbooleanconditional 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 tofalsefalse)||evaluates totruetrue)References
&,^, and|&&||