Java ProgramsBasics & I/ODemonstrate Short-Circuit OR

Demonstrate Short-Circuit OR in Java

intermediate·  Basics & I/O  ·  Operators

Problem

The || operator short-circuits: if the left operand is true, the overall result is already determined to be true, so Java never bothers evaluating the right operand at all — not even to compute a value it would then discard.

Given a true left-hand condition, prove that the right-hand side of || is never evaluated at all.

Input
left = true
Output
Result: true (right side never ran)

Java Program

Java
public class DemonstrateShortCircuitOr { static boolean rightSide() { System.out.println("Right side evaluated!"); return false; } public static void main(String[] args) { boolean left = true; System.out.println("Evaluating: left || rightSide()"); boolean result = left || rightSide(); // rightSide() is never called, since left is already true System.out.println("Result: " + result); } }

Output

Evaluating: left || rightSide() Result: true

Core Logic

Putting a visible print statement inside the right-hand method makes short-circuiting provable, not just theoretical — if that print never appears, the method genuinely never ran.

How It Works
  1. 1rightSide() prints a message before returning false, so if it's called, that message will show up in the output.
  2. 2left is true.
  3. 3left || rightSide() evaluates left first — since it's already true, the whole expression is guaranteed to be true no matter what rightSide() would return.
  4. 4Because the result is already decided, Java skips calling rightSide() entirely — its print statement never runs, which is why "Right side evaluated!" never appears in the output.
With left = true, left || rightSide() prints only Result: true — no message from rightSide() at all, proving it was never called.
💡

Key Point: This mirrors && short-circuiting on a false left operand — || short-circuits on a true one, which is why cache != null || rebuildCache() is a common pattern: the expensive rebuild only happens when it's actually needed.

Key Concepts

|| operatorshort-circuit evaluationlazy evaluation

Approach 2: Java 8

Java
import java.util.function.BooleanSupplier; public class ShortCircuitOrSupplier { public static void main(String[] args) { boolean left = true; // Nothing inside this lambda runs yet — it's just a stored, deferred computation BooleanSupplier rightSide = () -> { System.out.println("Right side evaluated!"); return false; }; System.out.println("Evaluating: left || rightSide.getAsBoolean()"); boolean result = left || rightSide.getAsBoolean(); // never invoked, since left is already true System.out.println("Result: " + result); } }

Output

Evaluating: left || rightSide.getAsBoolean() Result: true

Core Logic

Wrapping the right-hand side in a BooleanSupplier makes the deferred, maybe-never-run nature of that computation explicit as a value, instead of leaving it implicit in a plain method call.

How It Works
  1. 1BooleanSupplier rightSide = () -> { ...; return false; }; packages the same print-then-return logic as a lazy value — nothing inside the lambda runs yet, just by creating it.
  2. 2left || rightSide.getAsBoolean() still short-circuits exactly the same way as calling a method directly would — || doesn't care whether its right operand is a plain call or a stored BooleanSupplier.
  3. 3Since left is true, getAsBoolean() is never invoked, and the lambda's print statement never runs.
With left = true, the stored rightSide supplier is created but never invoked — the output is identical to the direct method-call version.
💡

Key Point: A BooleanSupplier makes the laziness visible as a value you can pass around, store, or reuse — but it doesn't change the short-circuit behavior itself, which comes from || alone, not from how the right-hand side happens to be written.

Key Concepts

BooleanSupplierfunctional interfacelazy evaluation

Related Programs