Most Java developers know that && means AND and || means OR.
But did you know these operators can skip evaluating part of your code?
This behavior is called short-circuit evaluation, and it's one of the reasons Java code becomes safer, faster, and more efficient.
In this article, you'll learn:
- What short-circuit operators are
- Difference between
&,|and&&,|| - How short-circuit evaluation works
- Step-by-step execution examples
- How it prevents runtime exceptions
- Real-world null check examples
- Interview questions and memory tricks
What are Short-Circuit Operators?
Java provides two short-circuit logical operators:
-
&&(Short-Circuit AND) -
||(Short-Circuit OR)
They behave like logical AND and OR, but with one important difference:
They may skip evaluating the second operand if the final result is already known.
This optimization is called short-circuit evaluation.
Difference Between &, | and &&, ||
Although & and | can also work with boolean values, they always evaluate both operands.
&& and || evaluate the second operand only when necessary.
Feature Comparison: Bitwise vs Logical Operators
================================================
Operators: `&`, `|`, `^` vs `&&`, `||`
─────────────────────────────────────────────
📌 Second operand always evaluated?
Bitwise (`&`, `|`) → ✅ Yes
Logical (`&&`, `||`) → ❌ No (Short Circuit)
📌 Performance
Bitwise (`&`, `|`) → Lower
Logical (`&&`, `||`) → Higher
📌 Works with boolean
Bitwise (`&`, `|`) → ✅ Yes
Logical (`&&`, `||`) → ✅ Yes
📌 Works with integral types
Bitwise (`&`, `|`) → ✅ Yes
Logical (`&&`, `||`) → ❌ No
Rule #1: Short-Circuit AND (&&)
The second operand is evaluated only if the first operand is true.
x && y
If x is false
↓
Result is already false
↓
Skip evaluating y
In Simple Words
- First operand is false → stop immediately.
- First operand is true → evaluate the second operand.
Example
System.out.println(false && true);
The second operand doesn't need to be evaluated because the answer is already false.
Rule #2: Short-Circuit OR (||)
The second operand is evaluated only if the first operand is false.
x || y
If x is true
↓
Result is already true
↓
Skip evaluating y
In Simple Words
- First operand is true → stop immediately.
- First operand is false → evaluate the second operand.
Example
System.out.println(true || false);
Since the first operand is already true, Java skips the second operand.
Understanding with an Example
int x = 10;
int y = 15;
if (++x < 10 || ++y > 15) {
x++;
} else {
y++;
}
System.out.println(x + " ---- " + y);
Output
12 ---- 16
Step-by-Step Execution (||)
Initial values
x = 10
y = 15
Evaluate
++x < 10 || ++y > 15
Step 1
++x
x = 11
Step 2
11 < 10
false
Since the first operand is false, Java must evaluate the second operand.
Step 3
++y
y = 16
Step 4
16 > 15
true
Expression becomes
false || true
↓
true
The if block executes.
x++;
Final values
x = 12
y = 16
Step-by-Step Execution (&&)
Now replace || with &&.
int x = 10;
int y = 15;
if (++x < 10 && ++y > 15) {
x++;
} else {
y++;
}
System.out.println(x + " ---- " + y);
Output
11 ---- 16
Execution
Step 1
++x
x = 11
Step 2
11 < 10
false
Since the first operand is false, Java already knows:
false && anything
↓
false
Therefore,
++y is NEVER executed.
The else block runs.
y++;
Final values
x = 11
y = 16
Notice that ++y > 15 was completely skipped.
Comparing &, |, && and ||
Using the same example:
int x = 10;
int y = 15;
1️⃣ & (Bitwise AND)
Final x → 11
Final y → 17
Reason → Both operands evaluated. Condition becomes false. else executes.
2️⃣ | (Bitwise OR)
Final x → 12
Final y → 16
Reason → Both operands evaluated. Condition becomes true. if executes.
3️⃣ && (Logical AND)
Final x → 11
Final y → 16
Reason → Second operand skipped because first is false.
4️⃣ || (Logical OR)
Final x → 12
Final y → 16
Reason → Second operand evaluated because first is false.
Why Short-Circuit Operators Matter
Short-circuit operators are not only faster—they can also prevent runtime exceptions.
Consider this example:
int x = 10;
if (++x < 10 && (x / 0 > 10)) {
System.out.println("Hello");
} else {
System.out.println("Hi");
}
Output
Hi
Why Doesn't This Throw ArithmeticException?
Evaluation begins with
++x < 10
11 < 10
↓
false
Now Java sees
false && ...
Since the result is already false, the second operand is never evaluated.
That means
x / 0
is never executed.
Therefore,
- ✅ No
ArithmeticException - ✅
elseblock executes - ✅ Output is
Hi
What if We Use & Instead?
if (++x < 10 & (x / 0 > 10))
The & operator always evaluates both operands.
So Java executes
x / 0
Result
Exception in thread "main"
java.lang.ArithmeticException: / by zero
This is one of the biggest practical advantages of &&.
Real-World Example: Null Check
One of the most common uses of short-circuit operators is preventing NullPointerException.
Safe code
String name = null;
if (name != null && name.length() > 0) {
System.out.println(name);
}
Why is this safe?
If
name == null
then Java immediately stops evaluating the condition.
It never calls
name.length()
so no exception occurs.
Unsafe Version
String name = null;
if (name != null & name.length() > 0) {
System.out.println(name);
}
Since & always evaluates both operands,
Java executes
name.length()
which throws
NullPointerException
Can && and || Work with Integers?
No.
The following code produces a compile-time error.
System.out.println(4 && 5);
System.out.println(4 || 5);
Compiler Error
operator && cannot be applied to int, int
operator || cannot be applied to int, int
However,
System.out.println(4 & 5);
System.out.println(4 | 5);
is perfectly valid because & and | also work with integral types.
Output
4
5
Master Summary Table
1️⃣ & (Bitwise AND)
Type → Bitwise AND
Short Circuit → ❌ No
boolean → ✅
Integral → ✅
2️⃣ | (Bitwise OR)
Type → Bitwise OR
Short Circuit → ❌ No
boolean → ✅
Integral → ✅
3️⃣ && (Short-Circuit AND)
Type → Short-Circuit AND
Short Circuit → ✅ Yes
boolean → ✅
Integral → ❌
4️⃣ || (Short-Circuit OR)
Type → Short-Circuit OR
Short Circuit → ✅ Yes
boolean → ✅
Integral → ❌
Interview Questions
Which operators support short-circuit evaluation?
✅ &&
✅ ||
When does && skip the second operand?
When the first operand is false.
When does || skip the second operand?
When the first operand is true.
Why is && preferred over & for boolean expressions?
Because it:
- Improves performance
- Avoids unnecessary computation
- Prevents runtime exceptions like
NullPointerExceptionandArithmeticException
Can && and || work with integers?
No.
They only work with boolean expressions.
Memory Trick 🧠
&& (AND)
False first? Stop immediately.
|| (OR)
True first? Stop immediately.
Easy Way to Remember
Short Circuit = Short Cut
If the first condition already decides the result, Java takes a shortcut and skips the remaining condition.
One-Line Summary
// x && y -> If x is false, y is NOT evaluated.
// x || y -> If x is true, y is NOT evaluated.
Key Takeaways
-
&&and||are called short-circuit operators because they may skip evaluating the second operand. -
&&skips the second operand when the first operand is false. -
||skips the second operand when the first operand is true. -
&and|always evaluate both operands and can also be used with integral types. - Short-circuit operators improve performance and help prevent runtime exceptions such as
NullPointerExceptionandArithmeticException. - Using
&&for null checks is a common Java best practice.
Happy Coding!
Top comments (0)