Java logical operator short circuit with example
Java has two sets of logical operators for boolean expressions: the short-circuit operators (&& and ||) and the non-short-circuit operators (& and |). They produce the same boolean result, but differ in when they evaluate the right operand. Understanding this difference matters for both correctness and performance.
How Short-Circuit Evaluation Works
| Operator | Name | Short-circuits when… |
|---|---|---|
&& | Conditional AND | Left side is false — result is false regardless of right side |
|| | Conditional OR | Left side is true — result is true regardless of right side |
& | Logical AND | Never — always evaluates both sides |
| | Logical OR | Never — always evaluates both sides |
Demonstrating Short-Circuit with &&
public class ShortCircuitDemo {
static boolean isFalse() {
System.out.println("isFalse() called");
return false;
}
static boolean isTrue() {
System.out.println("isTrue() called");
return true;
}
public static void main(String[] args) {
System.out.println("--- Using && ---");
boolean result = isFalse() && isTrue();
System.out.println("Result: " + result);
System.out.println("--- Using & ---");
result = isFalse() & isTrue();
System.out.println("Result: " + result);
}
}
Output:
--- Using && ---
isFalse() called
Result: false
--- Using & ---
isFalse() called
isTrue() called
Result: false
With &&, once isFalse() returns false, Java skips isTrue() entirely — the overall result is already determined. With &, both methods are always called.
Demonstrating Short-Circuit with ||
System.out.println("--- Using || ---");
boolean result = isTrue() || isFalse();
System.out.println("Result: " + result);
System.out.println("--- Using | ---");
result = isTrue() | isFalse();
System.out.println("Result: " + result);
Output:
--- Using || ---
isTrue() called
Result: true
--- Using | ---
isTrue() called
isFalse() called
Result: true
With ||, once isTrue() returns true, the right side is skipped. With |, both sides always run.
Practical Benefit: Null Safety
The most common real-world use of short-circuit evaluation is guarding against NullPointerException:
String text = null;
// Safe — if text is null, the second condition is never evaluated
if (text != null && text.length() > 5) {
System.out.println("Long string: " + text);
}
// Unsafe with & — both sides always run; throws NullPointerException
if (text != null & text.length() > 5) { // NPE!
System.out.println("Long string: " + text);
}
This pattern — checking for null before calling methods on an object — is ubiquitous in Java. It only works correctly because && short-circuits.
Practical Benefit: Avoiding Expensive Operations
Place the cheaper or most-likely-to-fail condition on the left side so the expensive one is skipped when possible:
// isEnabled() is a cheap boolean field check
// fetchFromDatabase() is an expensive I/O call
if (isEnabled() && fetchFromDatabase(id) != null) {
process();
}
// If isEnabled() is false, fetchFromDatabase() is never called
When to Use Non-Short-Circuit Operators (& and |)
The non-short-circuit & and | are rarely used with booleans, but they have a valid use case: when you need both sides to execute for their side effects.
// Both increment operations must run — use & instead of &&
int x = 0, y = 0;
boolean result = (++x > 0) & (++y > 0);
System.out.println("x=" + x + " y=" + y); // x=1 y=1
// With &&, y might not be incremented:
x = 0; y = 0;
result = (++x > 5) && (++y > 0); // x=1 but y=0 — y not incremented
In practice, code with side effects inside conditions is hard to read. Prefer computing side effects before the condition rather than relying on &.
Summary
Use && and || (short-circuit) in almost all boolean conditions — they're safer (null guards work), more efficient (skip unnecessary work), and what Java developers expect. Use & and | only when you explicitly need both sides to always evaluate. Put cheaper or fail-fast conditions on the left side of && to maximize the performance benefit of skipping the right side.