liveBook · Manning

Short-circuit evaluation

As explained in 100 Java Mistakes and How to Avoid Them

A short-circuit operator is a Java boolean operator that skips evaluation of its right operand when the result is already determined by the left operand.

Short-circuit operators && and || skip their right operand when the left operand already determines the result. Their non-short-circuit counterparts, & and |, always evaluate both operands. section 2.7

A bounds-checking typo

In `index >= 0 && index < data.length & data[index] > 0`, the final `&` forces `data[index]` to execute even when the index is out of bounds, causing ArrayIndexOutOfBoundsException. Use `&&` when the later check depends on the earlier one. section 2.7

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains operand evaluation, compound-assignment behavior, and precedence hazards through focused Java examples.
Examples
Uses array bounds checks, chained validation calls, mixed logical conditions, and independently evaluated update methods.
What it assumes
Assumes familiarity with Java boolean expressions and teaches the distinction between short-circuit and non-short-circuit operators.
What you can do after
Diagnose/debug with it by spotting eager operators and rewriting conditions or assignments that should skip later operands.
How it connects
Connects boolean operators with compound assignment operators, operator precedence, bounds checking, and runtime exceptions.

Also explained in

Compare how 3 books explain short-circuit evaluation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.