Java Logical and Boolean Operators: Short-circuit and Not-short-circuit

Java Logical and Boolean Operators Table

When two or more relational expressions are involved in a decision making statement, they are combined by logical operators. Logical operators come in two flavors - short circuit and not-short circuit. They both operate upon boolean values and they evaluate to boolean values. Short-circuit operators are called logical operators while not-short-circuit operators are called boolean operators.

Both short-circuit and not-short circuit operators perform logical operations on boolean expressions, but there is a difference between their operating styles - short circuit logical operators evaluate second expression only if this is needed. For example, if you want to perform a logical AND between expr-1 and expr-2 and if expr-1 yields false then no matter what expr-2 returns; the final result will be false. In that case expr-2; if left un-executed, will have no impact on final outcome of the AND operation. It makes the operation faster. On the contrary not-short circuit logical operation executes all boolean expressions given for execution.

For example, if A and B are two boolean expressions then following Java logical operators can be applied on expressions A and B.

Table 5: Demonstration: Java logical operators
Operation Description Type
A && B logical AND short circuit
A || B logical OR short circuit
A & B boolean logical AND not short circuit
A | B boolean logical OR not short circuit
A ^ B boolean logical exclusive OR
!A logical NOT

Short circuit logical operators are efficient and safe to use, that's why we usually do not see not-short circuit in programs.

Java Logical Operators (Short-circuit)

&& and || are Java's logical operators, these operators are also called conditional operators. They perform a boolean operation on their two boolean operands and evaluate to a boolean result.

Logical AND (&&)

Logical AND performs a Boolean AND operation on its operands. It evaluates to true if and only if both operands of logical AND are true. If either or both operands are false, it evaluates to false. For example:

// Evaluates to true, if both comparisons are true
if (x < 10 && y > 3) ...
Note that all logical and boolean operators except the unary ! operator have a lower precedence than the comparison operators. Thus, it is perfectly legal to write a line of code like the one above. However, some programmers prefer to use parentheses to make the order of evaluation explicit:
if ((x < 10) && (y > 3)) ...
Logical AND or conditional AND operator is called short-circuit operator because it conditionally evaluates its second operand. If the first operand evaluates to false, the value of the expression is false, regardless of the value of the second operand. Therefore, to increase efficiency, the Java interpreter takes a short-cut and skips the second operand. Since the second operand is not guaranteed to be evaluated, you must use caution when using this operator with expressions that have side effects. On the other hand, the conditional nature of this operator allows us to write Java expressions such as the following:
if (data != null && i < data.length && data[i] != -1) ...

Logical OR (||)

Logical OR operator performs a boolean OR operation on its two boolean operands. It evaluates to true if either or both of its operands are true. If both operands are false, it evaluates to false. Like the && operator, || does not always evaluate its second operand. If the first operand evaluates to true, the value of the expression is true, regardless of the value of the second operand. Thus, the operator simply skips that second operand in that case.

Java Boolean Operators (Not-short-circuit)

Boolean AND (&)

When used with boolean operands, the & operator behaves like the && operator, except that it always evaluates both operands, regardless of the value of the first operand. This operator is almost always used as a bitwise operator with integer operands, however, and many Java programmers would not even recognize its use with boolean operands as legal Java code.

Boolean OR (|)

This operator performs a boolean OR operation on its two boolean operands. It is like the || operator, except that it always evaluates both operands, even if the first one is true. The | operator is almost always used as a bitwise operator on integer operands; its use with boolean operands is very rare.

Boolean XOR (^)

When used with boolean operands, this operator computes the Exclusive OR (XOR) of its operands. It evaluates to true if exactly one of the two operands is true. In other words, it evaluates to false if both operands are false or if both operands are true. Unlike the && and || operators, this one must always evaluate both operands. The ^ operator is much more commonly used as a bitwise operator on integer operands. With boolean operands, this operator is equivalent to the != operator

Boolean NOT (!)

Boolean ! is a unary operator that changes the boolean value of its operand. If applied to a true value, it evaluates to false, and if applied to a false value, it evaluates to true. For ! is a unary operator, it has a high precedence and often must be used with parentheses. It is useful in expressions like these:

if (!found) ... // found is a boolean variable declared somewhere

while (!c.isEmpty()) ... // isEmpty() returns a boolean value

if (!(x > y && y > z))

Last Word

In this tutorial we discussed boolean and logical operators of Java. Hope you have enjoyed reading this tutorial on logical operators. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!

References



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

is the founder and main contributor for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.