Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The Java operators <code>&amp;</code>, <code>|</code> and <code>^</code> are EITHER bitwise operators OR logical operators ... depending on the types of the operands. If the operands are integers, the operators are bitwise. If they are booleans, then the operators are logical.</p> <p>And this is not just me saying this. The JLS describes these operators this way too; see <a href="http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.22" rel="noreferrer">JLS 15.22</a>.</p> <p>(This is just like <code>+</code> meaning EITHER addition OR string concatenation ... depending on the types of the operands. Or just like a "rose" meaning either a flower or a shower attachment. Or "cat" meaning either a furry animal or a UNIX command. Words mean different things in different contexts. And this is true for the symbols used in programming languages too.)</p> <hr> <blockquote> <p>There are already logical operators <code>&amp;&amp;</code>, <code>||</code>, why use <code>&amp;</code>, <code>|</code>, <code>^</code>?</p> </blockquote> <p>In the case of the first two, it is because the operators have different semantics with regards to when / whether the operands get evaluated. The two different semantics are needed in different situations; e.g.</p> <pre><code> boolean res = str != null &amp;&amp; str.isEmpty(); </code></pre> <p>versus</p> <pre><code> boolean res = foo() &amp; bar(); // ... if I &gt;&gt;need&lt;&lt; to call both methods. </code></pre> <p>The <code>^</code> operator has no short-circuit equivalent because it simply doesn't make sense to have one.</p>
 

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