skip to content

How do short-circuit operators (&&, ||) behave inside if conditions, and why does it matter?

level: middleimportance: should knowfreq 50%

answer

  1. && stops at first false; || stops at first true
  2. left-to-right, right operand may be skipped
  3. null-guard: check != null BEFORE dereferencing
  4. & and | are eager — always evaluate both sides
  5. avoid side effects in the right operand

basics

~20 s

&& and || stop evaluating as soon as the result is known: && stops at the first false, || stops at the first true. This lets you safely guard later checks, like checking for null before using a value.

solid answer

~50 s

Java's logical AND `&&` and OR `||` are **short-circuit** operators: they evaluate the left operand first and skip the right operand whenever the result is already determined. For `&&`, if the left is false the whole expression is false, so the right is not evaluated; for `||`, if the left is true the whole expression is true, so the right is skipped. This is heavily used in if conditions as a **guard**: `if (s != null && s.length() > 0)` is safe because when `s` is null the `length()` call is never reached, avoiding a NullPointerException. The order of operands therefore carries meaning. Java also has non-short-circuit `&` and `|`, which always evaluate both sides; they are rarely used on booleans and matter only when the right operand has intentional side effects you always want to run.

code

java · 5 lines
java
String name = getName(); // may return null
if (name != null && name.startsWith("A")) {
    System.out.println("Starts with A");
}
// startsWith is only called when name is non-null

go deeper

for a junior

Knows && means both true and || means at least one true, and can combine simple conditions.

for a middle

Explains short-circuit evaluation, uses it for null guards in the correct order, and knows & / | evaluate both sides.

for a senior

Reasons about side-effect placement, performance ordering (cheap checks first), and reviews conditions for short-circuit correctness.

for a principal

Sets conventions around guard clauses and side-effect-free conditions, and recognizes when complex boolean logic should be extracted into well-named predicate methods.

## Combining conditions If statements often need several conditions joined together. Java provides boolean operators for this. The two you'll use constantly are **`&&` (logical AND)** and **`||` (logical OR)**. - `&&` is true only when **both** sides are true. - `||` is true when **at least one** side is true. ## What "short-circuit" means These two operators are **short-circuiting**: they evaluate operands **left to right** and stop the moment the answer is certain. - For `A && B`: if `A` is **false**, the whole expression must be false regardless of `B`, so `B` is **never evaluated**. - For `A || B`: if `A` is **true**, the whole expression must be true regardless of `B`, so `B` is **never evaluated**. This is not just an optimization — it is a **guaranteed language behavior** you can rely on for correctness. ## The classic null-guard The most important practical use is guarding against errors: ```java if (s != null && s.length() > 0) { // use s } ``` A **NullPointerException (NPE)** is the error you get from calling a method on a `null` reference. Here, if `s` is null, `s != null` is false, and because of short-circuiting `s.length()` is **never called** — so no NPE. The operand order is essential: writing `if (s.length() > 0 && s != null)` would call `length()` *before* the null check and crash. Short-circuiting only protects you when the guard comes first. The `||` mirror image: ```java if (s == null || s.isEmpty()) { // treat as empty } ``` If `s` is null, `s == null` is true and `isEmpty()` is skipped — again safe. ## Side effects and ordering Because the right operand may be skipped, any **side effect** (a change to state, such as a method that increments a counter or writes a log) in the right operand might not happen. Putting side effects inside a short-circuit condition is therefore discouraged — it makes whether the side effect runs depend on the left operand. ## The non-short-circuit cousins: `&` and `|` Java also has `&` and `|`. On booleans these are **eager**: they always evaluate **both** operands, even when the result is already known. They are mainly bitwise operators for integers; using them on booleans is unusual and only justified when you deliberately want both sides (including their side effects) to always run. For ordinary conditions, prefer `&&` and `||`. ## How to derive the answer at any level Remember the slogan: "`&&` stops at the first false; `||` stops at the first true." Then read any compound condition left to right and ask "is the rest even reached?" — that tells you both the result and whether a later call is safe.

  • Why is operand order critical in `s != null && s.length() > 0`?
    Short-circuit only skips the right side. The null check must be the left operand so that when s is null, length() is never called. Reversing the order would dereference null and throw an NPE.
  • When might you intentionally use `&` instead of `&&` on booleans?
    Only when you want both operands evaluated regardless of the first result — typically because the right operand has a side effect that must always run. This is rare and usually clearer written as two separate statements.

saying these in an interview costs you the question

  • Putting the dereference before the null check and relying on short-circuit to save you
  • Assuming & and | short-circuit like && and ||
  • Hiding important side effects in a short-circuited right operand

context