Short Circuit Evaluation

In short: Short-circuit evaluation means a logical expression stops as soon as its result is determined. With AND, if the first operand is false the rest is skipped; with OR, if the first is true the rest is skipped, which is useful for guarding against errors.

Short-circuit evaluation is a strategy most programming languages (including Java) use to avoid unnecessary work. For example, say we had a conditional like this:

if (itIsFriday && itIsRaining) { System.out.println("board games at my place!"); }

Let's say itIsFriday is false. Because Java short-circuits evaluation, it wouldn't bother checking the value of itIsRaining—it knows that either way the condition is false and we won't print the invitation to board game night.

We can use this to our advantage. For example, say we have a check like this:

if (friends.get("Becky").isFreeThisFriday()) { inviteToBoardGameNight(friends.get("Becky")); }

What happens if "Becky" isn't in our friends hash map? Since friends.get("Becky") is null, when we try to call isFreeThisFriday we'll get a NullPointerException.

Instead, we could first confirm that Becky and I are still on good terms:

if (friends.containsKey("Becky") && friends.get("Becky").isFreeThisFriday()) { inviteToBoardGameNight(friends.get("Becky")); }

This way, if "Becky" isn't in friends, Java will skip the second check about Becky being free and avoid throwing the NullPointerException.

This is all hypothetical, of course. It's not like things with Becky are weird or anything. We're totally cool. She's still in my friends hash map for sure and I hope I'm still in hers and Becky if you're reading this I just want you to know you're still in my friends hash map.

Frequently Asked Questions

What is short-circuit evaluation?

When evaluating a boolean expression, the program stops as soon as the outcome is known: 'false AND anything' is false and 'true OR anything' is true, so the second operand is never evaluated.

Why is short-circuit evaluation useful?

It avoids unnecessary work and guards against errors; for example, 'node != null && node.value == x' won't dereference a null node.

Which operators short-circuit?

The logical AND (&&) and logical OR (||). The bitwise operators & and | do not short-circuit; they always evaluate both sides.

Last updated: June 17, 2026

What's next?

If you're ready to start applying these concepts to some problems, check out our mock coding interview questions.

They mimic a real interview by offering hints when you're stuck or you're missing an optimization.

Try some questions now

. . .