Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do &= and |= short-circuit in Java?

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.

like image 409
Justin K Avatar asked Jun 30 '10 18:06

Justin K


People also ask

What is the meaning of D o?

delivery order in British English (dɪˈlɪvərɪ ˈɔːdə ) business. a document that records an order for delivery of goods. ▶ USAGE The abbreviation for delivery order is D/O or d.o. Collins English Dictionary.

Do is a verb?

Do as an auxiliary verb. Do is one of three auxiliary verbs in English: be, do, have. We use do to make negatives (do + not), to make question forms, and to make the verb more emphatic.

What osteopathic means?

: a system of medical practice that emphasizes a holistic and comprehensive approach to patient care and utilizes the manipulation of musculoskeletal tissues along with other therapeutic measures (such as the use of drugs or surgery) to prevent and treat disease : osteopathic medicine.

Do is a verb or noun?

doing (noun) do–it–yourself (adjective) done. done (adjective)


2 Answers

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

  • Shortcut “or-assignment” (|=) operator in Java
  • What’s the difference between | and || in Java?
  • Why doesn’t Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||=)

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

  • JLS 15.22.2 Boolean Logical Operators &, ^, and |
  • JLS 15.23 Conditional-And Operator &&
  • JLS 15.24 Conditional-Or Operator ||
like image 107
polygenelubricants Avatar answered Oct 04 '22 16:10

polygenelubricants


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.

like image 28
Antal Spector-Zabusky Avatar answered Oct 04 '22 15:10

Antal Spector-Zabusky