DEV Community

Rajesh Bhola
Rajesh Bhola

Posted on

Java Short-Circuit Operators (&&, ||)

Most Java developers know that && means AND and || means OR.

But did you know these operators can skip evaluating part of your code?

This behavior is called short-circuit evaluation, and it's one of the reasons Java code becomes safer, faster, and more efficient.

In this article, you'll learn:

  • What short-circuit operators are
  • Difference between &, | and &&, ||
  • How short-circuit evaluation works
  • Step-by-step execution examples
  • How it prevents runtime exceptions
  • Real-world null check examples
  • Interview questions and memory tricks

What are Short-Circuit Operators?

Java provides two short-circuit logical operators:

  • && (Short-Circuit AND)
  • || (Short-Circuit OR)

They behave like logical AND and OR, but with one important difference:

They may skip evaluating the second operand if the final result is already known.

This optimization is called short-circuit evaluation.


Difference Between &, | and &&, ||

Although & and | can also work with boolean values, they always evaluate both operands.

&& and || evaluate the second operand only when necessary.

Feature Comparison: Bitwise vs Logical Operators
================================================

Operators:  `&`, `|`, `^`     vs     `&&`, `||`

─────────────────────────────────────────────

📌 Second operand always evaluated?
   Bitwise  (`&`, `|`)     →  ✅ Yes
   Logical  (`&&`, `||`)   →  ❌ No (Short Circuit)

📌 Performance
   Bitwise  (`&`, `|`)     →  Lower
   Logical  (`&&`, `||`)   →  Higher

📌 Works with boolean
   Bitwise  (`&`, `|`)     →  ✅ Yes
   Logical  (`&&`, `||`)   →  ✅ Yes

📌 Works with integral types
   Bitwise  (`&`, `|`)     →  ✅ Yes
   Logical  (`&&`, `||`)   →  ❌ No
Enter fullscreen mode Exit fullscreen mode

Rule #1: Short-Circuit AND (&&)

The second operand is evaluated only if the first operand is true.

x && y

If x is false
↓

Result is already false

↓

Skip evaluating y
Enter fullscreen mode Exit fullscreen mode

In Simple Words

  • First operand is false → stop immediately.
  • First operand is true → evaluate the second operand.

Example

System.out.println(false && true);
Enter fullscreen mode Exit fullscreen mode

The second operand doesn't need to be evaluated because the answer is already false.


Rule #2: Short-Circuit OR (||)

The second operand is evaluated only if the first operand is false.

x || y

If x is true
↓

Result is already true

↓

Skip evaluating y
Enter fullscreen mode Exit fullscreen mode

In Simple Words

  • First operand is true → stop immediately.
  • First operand is false → evaluate the second operand.

Example

System.out.println(true || false);
Enter fullscreen mode Exit fullscreen mode

Since the first operand is already true, Java skips the second operand.


Understanding with an Example

int x = 10;
int y = 15;

if (++x < 10 || ++y > 15) {
    x++;
} else {
    y++;
}

System.out.println(x + " ---- " + y);
Enter fullscreen mode Exit fullscreen mode

Output

12 ---- 16
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Execution (||)

Initial values

x = 10
y = 15
Enter fullscreen mode Exit fullscreen mode

Evaluate

++x < 10 || ++y > 15
Enter fullscreen mode Exit fullscreen mode

Step 1

++x

x = 11
Enter fullscreen mode Exit fullscreen mode

Step 2

11 < 10

false
Enter fullscreen mode Exit fullscreen mode

Since the first operand is false, Java must evaluate the second operand.

Step 3

++y

y = 16
Enter fullscreen mode Exit fullscreen mode

Step 4

16 > 15

true
Enter fullscreen mode Exit fullscreen mode

Expression becomes

false || true

↓

true
Enter fullscreen mode Exit fullscreen mode

The if block executes.

x++;
Enter fullscreen mode Exit fullscreen mode

Final values

x = 12

y = 16
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Execution (&&)

Now replace || with &&.

int x = 10;
int y = 15;

if (++x < 10 && ++y > 15) {
    x++;
} else {
    y++;
}

System.out.println(x + " ---- " + y);
Enter fullscreen mode Exit fullscreen mode

Output

11 ---- 16
Enter fullscreen mode Exit fullscreen mode

Execution

Step 1

++x

x = 11
Enter fullscreen mode Exit fullscreen mode

Step 2

11 < 10

false
Enter fullscreen mode Exit fullscreen mode

Since the first operand is false, Java already knows:

false && anything

↓

false
Enter fullscreen mode Exit fullscreen mode

Therefore,

++y is NEVER executed.
Enter fullscreen mode Exit fullscreen mode

The else block runs.

y++;
Enter fullscreen mode Exit fullscreen mode

Final values

x = 11

y = 16
Enter fullscreen mode Exit fullscreen mode

Notice that ++y > 15 was completely skipped.


Comparing &, |, && and ||

Using the same example:

int x = 10;
int y = 15;
Enter fullscreen mode Exit fullscreen mode

1️⃣ & (Bitwise AND)
Final x → 11
Final y → 17
Reason → Both operands evaluated. Condition becomes false. else executes.

2️⃣ | (Bitwise OR)
Final x → 12
Final y → 16
Reason → Both operands evaluated. Condition becomes true. if executes.

3️⃣ && (Logical AND)
Final x → 11
Final y → 16
Reason → Second operand skipped because first is false.

4️⃣ || (Logical OR)
Final x → 12
Final y → 16
Reason → Second operand evaluated because first is false.


Why Short-Circuit Operators Matter

Short-circuit operators are not only faster—they can also prevent runtime exceptions.

Consider this example:

int x = 10;

if (++x < 10 && (x / 0 > 10)) {
    System.out.println("Hello");
} else {
    System.out.println("Hi");
}
Enter fullscreen mode Exit fullscreen mode

Output

Hi
Enter fullscreen mode Exit fullscreen mode

Why Doesn't This Throw ArithmeticException?

Evaluation begins with

++x < 10
Enter fullscreen mode Exit fullscreen mode
11 < 10

↓

false
Enter fullscreen mode Exit fullscreen mode

Now Java sees

false && ...
Enter fullscreen mode Exit fullscreen mode

Since the result is already false, the second operand is never evaluated.

That means

x / 0
Enter fullscreen mode Exit fullscreen mode

is never executed.

Therefore,

  • ✅ No ArithmeticException
  • else block executes
  • ✅ Output is Hi

What if We Use & Instead?

if (++x < 10 & (x / 0 > 10))
Enter fullscreen mode Exit fullscreen mode

The & operator always evaluates both operands.

So Java executes

x / 0
Enter fullscreen mode Exit fullscreen mode

Result

Exception in thread "main"

java.lang.ArithmeticException: / by zero
Enter fullscreen mode Exit fullscreen mode

This is one of the biggest practical advantages of &&.


Real-World Example: Null Check

One of the most common uses of short-circuit operators is preventing NullPointerException.

Safe code

String name = null;

if (name != null && name.length() > 0) {
    System.out.println(name);
}
Enter fullscreen mode Exit fullscreen mode

Why is this safe?

If

name == null
Enter fullscreen mode Exit fullscreen mode

then Java immediately stops evaluating the condition.

It never calls

name.length()
Enter fullscreen mode Exit fullscreen mode

so no exception occurs.


Unsafe Version

String name = null;

if (name != null & name.length() > 0) {
    System.out.println(name);
}
Enter fullscreen mode Exit fullscreen mode

Since & always evaluates both operands,

Java executes

name.length()
Enter fullscreen mode Exit fullscreen mode

which throws

NullPointerException
Enter fullscreen mode Exit fullscreen mode

Can && and || Work with Integers?

No.

The following code produces a compile-time error.

System.out.println(4 && 5);
System.out.println(4 || 5);
Enter fullscreen mode Exit fullscreen mode

Compiler Error

operator && cannot be applied to int, int

operator || cannot be applied to int, int
Enter fullscreen mode Exit fullscreen mode

However,

System.out.println(4 & 5);
System.out.println(4 | 5);
Enter fullscreen mode Exit fullscreen mode

is perfectly valid because & and | also work with integral types.

Output

4

5
Enter fullscreen mode Exit fullscreen mode

Master Summary Table

1️⃣ & (Bitwise AND)
Type → Bitwise AND
Short Circuit → ❌ No
boolean → ✅
Integral → ✅

2️⃣ | (Bitwise OR)
Type → Bitwise OR
Short Circuit → ❌ No
boolean → ✅
Integral → ✅

3️⃣ && (Short-Circuit AND)
Type → Short-Circuit AND
Short Circuit → ✅ Yes
boolean → ✅
Integral → ❌

4️⃣ || (Short-Circuit OR)
Type → Short-Circuit OR
Short Circuit → ✅ Yes
boolean → ✅
Integral → ❌


Interview Questions

Which operators support short-circuit evaluation?

&&

||


When does && skip the second operand?

When the first operand is false.


When does || skip the second operand?

When the first operand is true.


Why is && preferred over & for boolean expressions?

Because it:

  • Improves performance
  • Avoids unnecessary computation
  • Prevents runtime exceptions like NullPointerException and ArithmeticException

Can && and || work with integers?

No.

They only work with boolean expressions.


Memory Trick 🧠

&& (AND)

False first? Stop immediately.


|| (OR)

True first? Stop immediately.


Easy Way to Remember

Short Circuit = Short Cut
Enter fullscreen mode Exit fullscreen mode

If the first condition already decides the result, Java takes a shortcut and skips the remaining condition.


One-Line Summary

// x && y  -> If x is false, y is NOT evaluated.

// x || y  -> If x is true, y is NOT evaluated.
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  • && and || are called short-circuit operators because they may skip evaluating the second operand.
  • && skips the second operand when the first operand is false.
  • || skips the second operand when the first operand is true.
  • & and | always evaluate both operands and can also be used with integral types.
  • Short-circuit operators improve performance and help prevent runtime exceptions such as NullPointerException and ArithmeticException.
  • Using && for null checks is a common Java best practice.

Happy Coding!

Top comments (0)