yawkat
yawkat

Reputation: 382

Does the logical AND operator have any use here?

I have been reading through the source code of an API implementation I use and stumbled upon this part of the code:

if (condition1 & condition2 && (condition3 || condition4))

Conditions 1 and 2 do not call any methods or perform any actions, while 3 and 4 do. I am wondering why the programmer decided not to use the short-circuit && operator for comparing the first two statements as I cannot see any benefit in using it (one more condition to check, the other two conditions which could actually have any influence on other parts of the program are still short-circuit).

Upvotes: 3

Views: 108

Answers (3)

Bohemian
Bohemian

Reputation: 425418

Without knowing how often condition1 is true, it would have been better (very slightly more efficient) to use &&, because the second operand condition2 doesn't need to be checked if condition1 is false, which may save a couple of CPU instructions.

If statistically condition1 is true more often than not, it's probably more efficient to use &, because the (usually unnecessary) short circuit check would be skipped.

In java you're allowed to use the bitwise and & with boolean operands to give a boolean result, but the only time there's a functional difference between using & and && is if code is called in the second operand, eg:

condition1 & <some code giving a boolean> // exdcutes "some code" regardless of condition1
condition1 && <some code giving a boolean> // only executes "some code" if condition1 is true

I would consider using & to always run <some code giving a boolean> to be in "side effect" territory, and not worth the confusion it would cause.

Upvotes: 3

arshajii
arshajii

Reputation: 129587

The only reason for doing this that I can think of would be to avoid the branch that is coupled with &&. You can see this in the bytecode:

a & b:

ILOAD 1
ILOAD 2
IAND

a && b:

ILOAD 1
IFEQ L3
ILOAD 2
IFEQ L3
ICONST_1
GOTO L4

It's certainly possible that there are cases where & marginally outperforms && as a result (specifically, when you're almost certain that the first condition will be true). Nevertheless, && is generally preferred because 1) such cases are rare and 2) the performance difference, in most contexts, would be completely negligible.

Upvotes: 5

Ben
Ben

Reputation: 421

There is actually one reason that the & operator could have been desired over &&, taking into account the point already discussed about & being just as fast if the conditions are boolean.

In the Java language specification, §15.7.

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation, and when code does not depend on exactly which exception arises as a consequence of the left-to-right evaluation of expressions.

If you check the order of operations, you will see that & comes before &&. Thus, this statement sticks to the principle of being clear about the order of evaluation without depending on Java evaluating left to right. This choice would be especially important if condition3 or condition4 call modifying methods - although this would be bad coding.

Upvotes: 1

Related Questions