Effect of a Bitwise Operator on a Boolean in Java
Using the bitwise operator can circumvent short-circuiting behavior:
boolean b = booleanExpression1() && booleanExpression2();
boolean b = booleanExpression1() & booleanExpression2();
If booleanExpression1()
evaluates to false
, thenbooleanExpression2()
is not evaluated in the first case, andbooleanExpression2()
(and whatever side-effects it may have) is evaluated in the second case,
The operators &
, ^
, and |
are bitwise operators when the operands are primitive integral types. They are logical operators when the operands are boolean, and their behaviour in the latter case is specified. See the section 15.22.2 of the Java Language Specification for details.