Logical Operators
& Short-Circuit
Master Java's boolean logic with &&, ||, and !: how they evaluate, when they skip work, and why it matters.
What Are Logical Operators?
Logical operators let you combine or negate boolean expressions. They take true/false values and produce a new true/false result.
&& AND|| OR! NOTWhy Logic Matters in Code
Almost every program needs decisions. Logical operators are the glue inside if, while, and for conditions that let you express complex rules in a single line.
Quick Boolean Refresher
A boolean expression is anything that evaluates to true or false. Comparison operators produce booleans:
The Truth Tables
What Is Short-Circuit Evaluation?
Java's && and || are short-circuit operators: they skip evaluating the right side whenever the left side alone determines the result.
Visualizing Short-Circuit Flow
Short-Circuit vs Non-Short-Circuit
Java also has & and | (single symbols). These always evaluate both sides: no short-circuiting.
| Short-Circuit | Non-Short-Circuit | |
|---|---|---|
| AND | && |
& |
| OR | || |
| |
| Skips right side? | Yes, when possible | Never |
| Side effects | Right side may not run | Both sides always run |
| Best practice | Use almost always | Rare, specialized use |
Operator Precedence
When multiple logical operators appear in one expression, Java follows a strict order. Parentheses always override precedence.
! (NOT): evaluated first, highest priority&& (AND): evaluated next|| (OR): evaluated last, lowest priorityHow to Evaluate Step by Step
&&, ||, or !?!)! before && before ||Example 1: Simple AND
&&, so evaluate right&& true → trueExample 2: Short-Circuit in Action
&& short-circuits → skip right side entirelyname.length() never executes → no NullPointerException!Example 3: Combined Operators
! first → !isStudent → !false → true|| (age >= 18 && hasID)|| is true: short-circuit! Skip the entire right sideage >= 18 nor hasID was evaluated.The Guard Pattern
Short-circuit evaluation is most powerful as a safety guard. Place a "safe to proceed?" check on the left, and the operation on the right.
Common Mistakes
= instead of ==if (x = 5) assigns rather than compares. Java catches this for booleans, but the intent is wrong.a || b && c and expecting (a || b) && c. The && binds tighter, so use parentheses.false && i++ means i is never incremented. Do not rely on evaluation order for side effects.Summary Cheat Sheet
| Operator | Name | Result | Short-Circuits When |
|---|---|---|---|
&& |
Logical AND | true if both true | Left is false |
|| |
Logical OR | true if either true | Left is true |
! |
Logical NOT | Flips value | N/A (unary) |
& |
Logical / Bitwise AND | true if both true | Never |
| |
Logical / Bitwise OR | true if either true | Never |
Question 1 of 5
Given int x = 3; and int y = 7;, what does this expression evaluate to?x > 5 && y < 10
&& is false, the operator short-circuits: the right side y < 10 is never evaluated, and the overall result is false.Question 2 of 5
What is the value of result after this code runs?boolean result = true || false && false;
&& binds tighter than ||, so this is true || (false && false). The inner expression is false. Then true || false is true. Also, || short-circuits because the left side is true.Question 3 of 5
What happens when this code executes?String s = null;if (s != null && s.length() > 0) { ... }
s != null evaluates to false, so && short-circuits and never evaluates s.length(). No NullPointerException is thrown. The condition evaluates to false safely.Question 4 of 5
Given int x = 5;, what is the value of x after this line?boolean b = (x > 10) && (++x > 5);
x > 10 is false (5 is not greater than 10). Because && short-circuits, ++x never executes. So x stays at 5. This is exactly why putting side effects (like ++) inside short-circuited expressions is dangerous.Question 5 of 5
What does !(true && false) || true evaluate to?
true && false is false. Then !false is true. Finally true || true is true. The || also short-circuits here since its left operand is already true.