Programming Tutorials and Interview Questions

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.

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.

`&&`

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 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) ...

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 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.

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.

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.

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 `!`

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))

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!

Rory visits a beekeeper in Manchester who is gluing wireless chips to his bees.

The new Apple's iOS has Persian keyboard. BBC Persian's Sam Farzaneh discusses why it is an important feature.

Ride-hailing app Uber is "not fit and proper" to operate in London, the transport regulator says.

×