01 / 21
&& || !

Logical Operators
& Short-Circuit

Master Java's boolean logic with &&, ||, and !: how they evaluate, when they skip work, and why it matters.

Java

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
True only when both sides are true
|| OR
True when at least one side is true
! NOT
Flips a boolean to its opposite

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

if (age >= 16 && hasPermit) { System.out.println("You may drive."); }

Quick Boolean Refresher

A boolean expression is anything that evaluates to true or false. Comparison operators produce booleans:

int x = 10; boolean a = x > 5; // true boolean b = x == 3; // false boolean c = x != 10; // false

The Truth Tables

\(A \&\& B\)
A B Result T T true T F false F T false F F false
\(A \ ||\ B\)
A B Result T T true T F true F T true F F false
\(!A\)
A Result T false F true Both must be true At least one true Flips the value

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.

&& Short-Circuit
If the left side is false, the result is already false: skip the right side
|| Short-Circuit
If the left side is true, the result is already true: skip the right side

Visualizing Short-Circuit Flow

\(A \&\& B\)
Evaluate A false Result: false B never runs true Evaluate B Result = B
\(A \ ||\ B\)
Evaluate A true Result: true B never runs false Evaluate B Result = B Key: evaluated skipped "Short-circuit" = the skipped path

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.

1
! (NOT): evaluated first, highest priority
2
&& (AND): evaluated next
3
|| (OR): evaluated last, lowest priority

How to Evaluate Step by Step

1
Identify the operator: is it &&, ||, or !?
2
Evaluate the left operand first (or the single operand for !)
3
Check for short-circuit: Can you already determine the result?
4
If not, evaluate the right operand and combine
5
Apply precedence: ! before && before ||

Example 1: Simple AND

int score = 85; boolean passed = true; boolean result = score > 70 && passed;
1
Evaluate left: score > 70 → 85 > 70 → true
2
Left is true: no short-circuit for &&, so evaluate right
3
Evaluate right: passed → true
4
true && true → true

Example 2: Short-Circuit in Action

String name = null; if (name != null && name.length() > 0) { System.out.println(name); }
1
Evaluate left: name != null → null != null → false
2
Left is false: && short-circuits → skip right side entirely
3
Result: false. name.length() never executes → no NullPointerException!

Example 3: Combined Operators

int age = 20; boolean isStudent = false; boolean hasID = true; boolean canEnter = !isStudent || (age >= 18 && hasID);
1
Precedence: ! first → !isStudent → !false → true
2
Now we have: true || (age >= 18 && hasID)
3
Left of || is true: short-circuit! Skip the entire right side
4
Result: true. Neither age >= 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.

// Guard against array out of bounds if (i >= 0 && i < arr.length && arr[i] == target) { // safe to access arr[i] } // Guard against dividing by zero if (denominator != 0 && numerator / denominator > 1) { // safe to divide }

Common Mistakes

✕ Using = instead of ==
Writing if (x = 5) assigns rather than compares. Java catches this for booleans, but the intent is wrong.
✕ Forgetting precedence
Writing a || b && c and expecting (a || b) && c. The && binds tighter, so use parentheses.
✕ Putting side effects in short-circuited code
Writing 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

Question 2 of 5

What is the value of result after this code runs?

boolean result = true || false && false;

Question 3 of 5

What happens when this code executes?

String s = null;
if (s != null && s.length() > 0) { ... }

Question 4 of 5

Given int x = 5;, what is the value of x after this line?

boolean b = (x > 10) && (++x > 5);

Question 5 of 5

What does !(true && false) || true evaluate to?