Scrivio Interview library
ConceptPrincipal6 min read · updated 12 November 2026

Java defines strict left-to-right evaluation of operands and short-circuit && and ||, while C and C++ leave much evaluation order unspecified. Why did Java choose this, and what does that predictability mean for how you write coding standards about side effects in expressions?

Java fixed operand evaluation to strict left to right, and made && and || short-circuit, so that a program means the same thing on every JVM and with every compiler, which supports its write-once, run-anywhere goal and makes code easier to reason about. The JIT may still reorder work internally, but only when the observable result matches the language rules. That predictability makes side effects in expressions legal and deterministic, but not readable, so coding standards should still keep conditions and compound expressions free of side effects, while relying on short-circuiting for documented guard idioms such as null checks.

Why this question is asked

Most engineers never think about evaluation order until a puzzle like i++ + ++i or a surprising log line forces them to. At a senior level, the interesting question is not the rule itself but why the language designers chose it, what it costs, and what it implies for the standards we set for large teams. Interviewers ask this to see whether a candidate can reason from language design to engineering practice. In this answer, we will recap what C and C++ leave unspecified, see what Java guarantees and how the JIT stays within it, weigh the trade-off the designers made, and then turn that into concrete guidance on side effects in expressions.

1A Quick Recap: What C and C++ Leave Open

In C, the order in which the operands of most operators are evaluated is unspecified: in f() + g(), the compiler may call g first. Worse, modifying the same variable twice between two sequence points, the points where all earlier side effects must be complete, is undefined behaviour, so i = i++ + ++i may produce any value, and the program as a whole has no defined meaning. C++ tightened some rules in C++17, for example for assignment and the stream operators, but much remains unspecified.

The reason C left it open was performance on the hardware of the 1970s and 1980s: compilers could choose the evaluation order that used the fewest registers or best fitted the instruction set. The price was programs that behaved differently with different compilers or optimisation levels.

Evaluation rules compared
AspectC / C++Java
Order of f() + g()unspecifiedf() first, guaranteed
i++ + ++iundefined behaviourdefined: always the same value
Arguments to a callunspecified orderleft to right
&& and || short-circuityes, guaranteedyes, guaranteed
Optimiser freedomlarge, visible to programsonly when the result is unchanged
Both languages guarantee short-circuiting; only Java also fixes the order of everything else.

2What Java Guarantees

The Java Language Specification states that the left operand of a binary operator is fully evaluated before any part of the right operand, that all operands are evaluated before the operation, that method arguments are evaluated left to right, and that && and || evaluate their right operand only when needed. Exceptions are part of this too: if the left operand throws, the right operand is never evaluated.

Java
int i = 1;
int r = i++ + ++i; // 1 + 3: always 4, on every JVM
int[] a = {0, 0};
int k = 0;
a[k] = k = 1; // a[0] = 1: the index is evaluated before the right side
System.out.println(a[0] + "," + a[1]); // 1,0
log(first(), second()); // first() always runs before second()

Each of these lines would be a portability bug in C. In Java they are deterministic. Notice the array example in particular: the array reference and index are evaluated before the right-hand side, so even though k becomes 1, the assignment goes to a[0].

3How the JIT Keeps the Promise

A fixed order does not mean the machine runs instructions in that order. The JIT compiler, and the CPU underneath it, reorder, combine and eliminate operations freely, under one constraint: within a single thread, the result must be as if the program had been executed in the order the language specifies. This is called as-if-serial semantics. If two operands have no side effects that affect each other, the JIT can compute them in any order or in parallel, because nobody can tell the difference. If one has a side effect the other can observe, the JIT must respect the order.

Step throughWhere evaluation order is decided
  1. Java sourceprogrammer writes f() + g()
  2. Language rules (JLS)f() before g(); short-circuit && and ||
  3. Bytecodejavac emits calls in that order
  4. JIT compilermay reorder only when no thread-local result changes
  5. CPUout-of-order execution, still as-if-serial within a thread

5 layers, from the top down. Step through them one at a time.

Each layer below may optimise, but none may change what the layer above defined.

So Java's choice costs little in practice. The cases where a free order would help an optimiser are mostly cases where the operands are independent, and there the JIT already has the freedom. Between threads, the separate Java Memory Model defines what other threads can see, which is a different question from evaluation order inside one expression.

4Legal and Deterministic Is Not the Same as Readable

The predictability gives us something valuable: no code has undefined meaning, so bugs can always be reproduced. But it does not make code with side effects in expressions easy to read. A reader who sees if (queue.poll() != null && queue.poll().isUrgent()) has to notice that poll() is called twice and removes two different elements. The language says exactly what happens; the reader just does not expect it.

Java
// Deterministic, but misleading: two different elements are removed
if (queue.poll() != null && queue.poll().isUrgent()) { handleUrgent(); }
// Clear: one side effect, as its own statement
Task next = queue.poll();
if (next != null && next.isUrgent()) { handleUrgent(next); }

Readers expect a condition to ask a question, not to change the world. When a condition both asks and changes, every reader must simulate the evaluation order in their head, and every refactoring, such as reordering operands for performance, risks changing behaviour.

5What This Means for Coding Standards

So the standard I would set has two sides. First, keep conditions and compound expressions free of side effects: no increments, assignments, removing calls, or methods that record metrics or write data inside if conditions, boolean chains or arithmetic with other operands. Do the side effect in its own statement, store the result in a well-named local, and then test it. Single, idiomatic exceptions, such as while ((line = reader.readLine()) != null), can be listed explicitly so the rule does not fight well-known patterns.

Second, rely on short-circuiting where it is a documented, common idiom. Guards such as s != null && s.isEmpty(), i < arr.length && arr[i] > 0, or map.containsKey(k) || compute(k) are not tricks; they are guaranteed by the specification and recognised by every Java reader. Banning them would make code worse. Static analysis supports both halves: tools such as Error Prone, Sonar and the IDE flag assignments in conditions, x = x++ and side effects in boolean operands, while leaving null-check guards alone. The underlying principle is the same one Java's designers applied: make behaviour predictable for every reader, not only for the compiler.

6Follow-up questions

Did fixed evaluation order cost Java performance?

Very little in practice. As-if-serial semantics let the JIT reorder independent operations, which covers most optimisation opportunities, and modern JITs work on a whole method or more, where expression-level order matters little. The remaining cost is rare and is generally considered worth the portability.

Is it safe to rely on argument evaluation order in method calls?

It is guaranteed, so code like log(nextId(), nextId()) behaves the same everywhere. But the standard still applies: two side effects in one argument list are easy to misread, so it is clearer to compute each value into a local first.

Does evaluation order say anything about multithreading?

No. It defines order within one thread. What other threads observe, and when, is governed by the Java Memory Model through happens-before relationships created by volatile, synchronized, locks and thread start and join. Confusing the two is a common source of concurrency misconceptions.

7Conclusion

In this answer, we saw that C and C++ leave much evaluation order unspecified, and some cases undefined, for optimisation freedom, while Java fixes left-to-right operand evaluation and short-circuit && and ||, so programs behave the same on every JVM and compiler. The JIT still reorders work under as-if-serial semantics, so the cost is small. That predictability makes side effects in expressions legal and deterministic but not readable, so standards should keep conditions free of side effects while embracing guaranteed guard idioms such as null checks before a call.

What to say in the interviewAbout 100 seconds aloud

Open with

Java's designers wanted a program to mean exactly the same thing on every JVM and every compiler, which fits write once, run anywhere. C and C++ leave much evaluation order unspecified, so in f() plus g() either may run first, and things like i++ plus ++i are undefined behaviour, all to give compilers freedom on old hardware.

Then, if they want more

Java instead says the left operand is fully evaluated before the right, arguments go left to right, and && and || short-circuit. The cost is small, because the JIT and the CPU still reorder work under as-if-serial semantics: within a thread they can do anything that does not change the observable result, which covers independent operands.

And this is about one thread; what other threads see is the memory model's job. For coding standards, the lesson is that predictable is not the same as readable. Side effects inside expressions are legal and deterministic in Java, but readers expect conditions to ask questions, not change state.

Something like queue.poll() != null && queue.poll().isUrgent() is perfectly defined and still removes two elements by surprise. So I would keep side effects out of conditions and compound expressions, put them in their own statements with well-named locals, allow a few listed idioms like the readLine loop, and use static analysis to flag the rest.

At the same time, I would explicitly endorse short-circuit guards like null checks before a call, because they are guaranteed by the specification and every Java developer recognises them.

About this answer

Last updated
12 November 2026
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 "Java defines strict left-to-right evaluation of operands and short-circuit && and ||...": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/51be003b5c/