Did you know that Java has three "and" operators?

First, there is the very common logical and&&:

if (x >= min && x <= max) { … }

Then, there is the bitwise and operator, which very few
programmers will probably ever use:

if (x & 1 != 0) { … }

And finally, there is…

public static boolean isBetween(int x, int min, int max) {
  boolean result = (x >= min) & (x <= max);
  return result;

Take a close look at this code.  The expression calculated for
uses what looks like the bitwise and operator, and yet it
seems to operate on boolean values (respectively x >= min and x <=
) and not scalar ones. 

There are actually two startling things in this code:

  1. & operating on booleans.
  2. The result of & being a boolean, and not a scalar.

The solution to the puzzle is given in the Java Language Specification,
chapter 15.22.2:

15.22.2 Boolean Logical Operators &, ^, and |

When both operands of
a &, ^, or | operator are of type
boolean, then the type of the bitwise operator expression is
For &, the result value is true if both operand
values are true; otherwise, the result is false.
For ^, the result value is true if the operand
values are different; otherwise, the result is false.
For |, the result value is false if both operand
values are false; otherwise, the result is true.

For all intents and purposes, it looks like the boolean logical &
operator is exactly similar to the logical && operator.  Is that
so?  Not really.  If you run the following program:

public static boolean f() {
  return false;

public static void main(String[] args) {
  boolean a = f() && f();
  boolean b = f() & f();

You will get the following result:


As you can see, && will skip the second expression if the first
expression is false (because at this point, it knows that the overall result
will be false, so there is no point in evaluating the remainder of the
expression) while & will always evaluate all its operands. 
Similarly, || will stop evaluating its operands as soon as one is found
to be true while | will always evaluate all of them.

Not surprisingly, logical operators are the most useful because not only do
they typically run faster than their boolean counterparts, they also allow to
write convenient expressions:

if (p != null && p.hasChildren()) { … }

With that in mind, when would you want to use a boolean logical & or
| operator?

The theoretical answer is:  whenever your code depends on side effects
performed by the operators involved in the expression and you want to make sure
that all of them are invoked, regardless of the final result of the expression.

Why do I say "theoretical"?  Such code would probably qualify as
a design smell because having methods that return a value and perform
side effects are usually frowned upon.  Having said that, it is not an
absolute rule and I have found myself having to do this a few times, so I would
rather discourage the use of boolean logical operators for a totally different
reason:  don’t use them because they will confuse readers of your code.

Can someone come up with a convincing use for a boolean logical operator?