Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

Explain the difference between && and & (and between || and |) on boolean operands, with an example where choosing the wrong one causes a NullPointerException.

&& and || are short-circuit operators: they evaluate the right operand only when the left one has not already decided the result, so false && anything is false without looking at anything, and true || anything is true. & and | on booleans always evaluate both operands. That is why if (s != null & s.isEmpty()) throws a NullPointerException when s is null, while if (s != null && s.isEmpty()) is safe: with && the call s.isEmpty() is never reached.

Why this question is asked

Null checks combined with a method call appear in almost every Java method, and they only work because of short-circuit evaluation. Writing a single & instead of && is an easy typo that compiles without a warning and then fails only for the input the check was meant to handle. Interviewers ask this basic question to make sure we know that the two operators differ in evaluation, not in result, and when that difference matters. In this answer, we will recap the four operators, see how short-circuiting works step by step, reproduce the NullPointerException, and look at the rare cases where the non-short-circuit form is used and what to write instead.

1A Quick Recap of the Four Operators

On boolean operands, && and & both compute logical and: the result is true only when both sides are true. || and | both compute logical or: true when at least one side is true. For the same two values, they always give the same result. The difference is whether the right-hand side is evaluated at all.

&& and || are called conditional or short-circuit operators. They look at the left operand first, and if that alone decides the result, they skip the right operand. & and | are the logical operators, which on ints are also the bitwise operators; on booleans they always evaluate both sides, left first, then combine.

When is the right operand evaluated?
ExpressionLeft is trueLeft is false
left && rightright is evaluatedright is skipped, result false
left & rightright is evaluatedright is evaluated, result false
left || rightright is skipped, result trueright is evaluated
left | rightright is evaluated, result trueright is evaluated
Same results, different work: only the short-circuit forms can skip the right side.

2Short-Circuiting Step by Step

We can watch the evaluation by giving each operand a side effect that prints.

Java
static boolean left(boolean v) { System.out.println("left"); return v; }
static boolean right(boolean v) { System.out.println("right"); return v; }
boolean r1 = left(false) && right(true); // prints: left r1 = false
boolean r2 = left(false) & right(true); // prints: left, right r2 = false
boolean r3 = left(true) || right(false); // prints: left r3 = true
boolean r4 = left(true) | right(false); // prints: left, right r4 = true

Notice that each pair returns the same value, but the short-circuit form printed only left. The reason is that the left operand already decided the answer: false and anything is false, true or anything is true, so evaluating the right side would be wasted work. Java guarantees this skipping; it is part of the language, not an optimisation the JIT might or might not do. That guarantee is what lets us use the left operand as a guard for the right one.

3The NullPointerException From a Single &

The classic guard is a null check followed by a method call on the same reference. With && the guard works; with & it does not.

Java
String s = null;
if (s != null && s.isEmpty()) { } // fine: s != null is false, s.isEmpty() is skipped
if (s != null & s.isEmpty()) { } // NullPointerException: s.isEmpty() runs anyway
if (s == null || s.isEmpty()) { // fine: s == null is true, s.isEmpty() is skipped
System.out.println("blank"); // blank
}
if (s == null | s.isEmpty()) { } // NullPointerException
Step throughEvaluating s != null && s.isEmpty() with s = null
  1. Evaluate s != nullfalse
  2. Is the left side false?
    YesResult is false; skip the right sideNoEvaluate s.isEmpty()
  3. With & insteads.isEmpty() runs anyway and throws NPE

3 steps in order. Play them, or step through them one at a time.

The guard only protects the call when the operator can skip it.

With &, Java evaluates s != null, gets false, and then still evaluates s.isEmpty() on a null reference, which throws. Since Java 14, helpful NullPointerException messages name the problem, for example Cannot invoke "String.isEmpty()" because "s" is null (the variable name appears when the class is compiled with -g; otherwise it shows a placeholder such as <local1>), which makes this bug quick to find once it happens. The point is that it should not happen: the null check exists precisely to protect the call, and only the short-circuit operator lets it do that.

The same guard pattern protects other operations that can fail: i < arr.length && arr[i] > 0 avoids ArrayIndexOutOfBoundsException, and count != 0 && total / count > 10 avoids division by zero.

4When the Non-Short-Circuit Form Is Used

& and | on booleans are occasionally used on purpose, when both operands have side effects that must always run. A typical example is validating two fields and wanting both error messages.

Java
boolean ok = validateName(form) & validateEmail(form); // both run, both record their errors
// Clearer: separate statements
boolean nameOk = validateName(form);
boolean emailOk = validateEmail(form);
boolean ok2 = nameOk && emailOk;

The first line works, but a reader may think the single & is a typo and helpfully change it to &&, which silently stops the second validation when the first fails. The second version makes the intent obvious: both validations are statements of their own, and the combination has no side effects left. So the practical rule is to use && and || for conditions, and when every operand must run, to say so with separate statements rather than relying on & or |.

5Follow-up questions

Do & and | have the same precedence as && and ||?

No. & binds more tightly than ^, which binds more tightly than |, and all three bind more tightly than && and ||. So a || b & c means a || (b & c). Mixing them without parentheses is confusing, which is another reason to keep & and | for bit manipulation.

Is there a performance difference?

Short-circuit forms can be faster because they skip work, and they are what the JIT expects for conditions. Non-short-circuit forms avoid a branch and are sometimes used in very hot, branch-sensitive code on cheap operands, but that is a micro-optimisation to measure, not a default.

What about ^ on booleans?

^ is exclusive or: true when exactly one side is true. It must always evaluate both operands, because the result depends on both, so it has no short-circuit version.

6Conclusion

In this answer, we saw that && and || give the same results as & and | on booleans but skip the right operand when the left one already decides the result, which Java guarantees. That guarantee makes guards like s != null && s.isEmpty() safe, while the single & evaluates s.isEmpty() on a null reference and throws a NullPointerException. The non-short-circuit forms are only for cases where every operand must run, and those are clearer as separate statements.

What to say in the interviewAbout 90 seconds aloud

Open with

Both pairs compute the same logical result on booleans; the difference is evaluation. && and || are short-circuit operators: they evaluate the left side first and skip the right side when the left already decides the answer.

Then, if they want more

False and anything is false, true or anything is true. & and | on booleans always evaluate both sides. That matters whenever the right side depends on the left being true, the typical case being a null check. If I write s != null && s.isEmpty(), and s is null, the left side is false, so isEmpty is never called.

If I accidentally write a single ampersand, Java evaluates s != null, gets false, and then still calls s.isEmpty() on null, so I get a NullPointerException exactly for the input the check was supposed to handle. The same pattern protects array indexes and division, like count != 0 && total / count > 10.

The non-short-circuit forms are sometimes used when both sides have side effects that must run, for example two validations that each record errors. But I prefer to write those as separate statements, because a reader may take the single ampersand for a typo and fix it to a double one, which silently skips the second validation.

So for conditions I always use && and ||, and I keep & and | for bit manipulation on integers.

About this answer

Last updated
12 November 2026
Versions it names
Java 14
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain the difference between && and & (and between || and |) on boolean operands, with...": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/89b5ae19e9/