Short-Circuit Evaluation with && and || in Java
Fast recall
Quick revision
Core answer
false left side skips the right; with `||`, a true left side skips the right. This is both a performance optimization and the standard null-safety pattern, e.g. obj != null && obj.isValid().Whether the candidate knows logical operators have execution-order implications beyond boolean logic, especially for null-safety guards
Assuming both sides of && or || always execute, leading to NullPointerExceptions or side effects that silently never run
Show the null-safety pattern obj != null && obj.getName().equals("foo") and explain why order matters because evaluation is left-to-right
Interview-ready guide
Interview answer
Short-circuit evaluation means Java stops checking a logical expression the instant it knows the result. With &&, a false on the left makes the whole thing false, so the right side is skipped. With ||, a true on the left does the same.
Why this matters more than performance is null safety. Writing if (obj != null && obj.method()) works because when obj is null the left side is false, so Java never calls the method — the null check guards everything to its right. That makes order critical: put the cheap, safe check on the left.
The flip side is a trap — if you rely on a side effect on the right, like a counter++, it silently won't run when the left short-circuits. And & isn't a shortcut for &&; it always evaluates both sides, so using it here brings back the very null-pointer crash you were trying to avoid.
Short-Circuit Evaluation with && and || in Java
Direct Answer
Short-circuit evaluation means Java stops evaluating a boolean expression as soon as the result is certain: with &&, a false left side skips the right; with ||, a true left side skips the right. This is both a performance optimization and the standard null-safety pattern, e.g. obj != null && obj.isValid().
Common Mistakes & Pitfalls
- Putting the null check after the method call —
user.length() > 0 && user != nullthrowsNullPointerExceptionbecause the left side runs first; the guard must come before what it protects. - Relying on a side effect that gets skipped — in
if (cheap() && counter++ > 0), whencheap()isfalsethecounter++never runs; short-circuiting silently drops the right side, which can leave counters or logs out of sync. - Using
&or|for boolean conditions —user != null & user.isActive()compiles, but&evaluates both sides, so the null check no longer protects the method call and you get an NPE at runtime. - Assuming
||is just&&reversed —&&short-circuits onfalse,||short-circuits ontrue; mixing up which one stops early leads to the wrong branch running or expensive work happening when it should not.
Check your understanding
Answer these without looking back at the article.
- What is the difference between & and && in Java?
- Can short-circuit evaluation cause a method with side effects to not execute?
- When would you intentionally use & instead of && ?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.