Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||=)
    primarykey
    data
    text
    <p>So for binary operators on booleans, Java has <code>&amp;</code>, <code>|</code>, <code>^</code>, <code>&amp;&amp;</code> and <code>||</code>.</p> <p>Let's summarize what they do briefly here:</p> <ul> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.22.2" rel="noreferrer">JLS 15.22.2 Boolean Logical Operators &amp;, ^, and |</a></li> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.23" rel="noreferrer">JLS 15.23 Conditional-And Operator &amp;&amp;</a> </li> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.24" rel="noreferrer">JLS 15.24 Conditional-Or Operator ||</a></li> </ul> <blockquote> <p>For <code>&amp;</code>, the result value is <code>true</code> if both operand values are <code>true</code>; otherwise, the result is <code>false</code>.</p> <p>For <code>|</code>, the result value is <code>false</code> if both operand values are <code>false</code>; otherwise, the result is <code>true</code>.</p> <p>For <code>^</code>, the result value is <code>true</code> if the operand values are different; otherwise, the result is <code>false</code>.</p> <p>The <code>&amp;&amp;</code> operator is like <code>&amp;</code> but evaluates its right-hand operand only if the value of its left-hand operand is <code>true</code>.</p> <p>The <code>||</code> operator is like <code>|</code>, but evaluates its right-hand operand only if the value of its left-hand operand is <code>false</code>.</p> </blockquote> <p>Now, among all 5, 3 of those have compound assignment versions, namely <code>|=</code>, <code>&amp;=</code> and <code>^=</code>. So my question is obvious: why doesn't Java provide <code>&amp;&amp;=</code> and <code>||=</code> as well? I find that I need those more than I need <code>&amp;=</code> and <code>|=</code>.</p> <p>And I don't think that "because it's too long" is a good answer, because Java has <code>&gt;&gt;&gt;=</code>. There must be a better reason for this omission.</p> <hr> <p>From <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.26" rel="noreferrer">15.26 Assignment Operators</a>:</p> <blockquote> <p>There are 12 assignment operators; [...] <code>= *= /= %= += -= &lt;&lt;= &gt;&gt;= &gt;&gt;&gt;= &amp;= ^= |=</code></p> </blockquote> <hr> <p>A comment was made that if <code>&amp;&amp;=</code> and <code>||=</code> were implemented, then it would be the only operators that do not evaluate the right hand side first. I believe this notion that a compound assignment operator evaluates the right hand side first is a mistake.</p> <p>From <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.26.2" rel="noreferrer">15.26.2 Compound Assignment Operators</a>:</p> <blockquote> <p>A compound assignment expression of the form <code>E1 op= E2</code> is equivalent to <code>E1 = (T)((E1) op (E2))</code>, where <code>T</code> is the type of <code>E1</code>, except that <code>E1</code> is evaluated only once. </p> </blockquote> <p>As proof, the following snippet throws a <code>NullPointerException</code>, not an <code>ArrayIndexOutOfBoundsException</code>.</p> <pre><code> int[] a = null; int[] b = {}; a[0] += b[-1]; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload