liveBook · Manning

Non-short-circuit operator

As explained in 100 Java Mistakes and How to Avoid Them

A non-short-circuit operator is a Java boolean operator that evaluates both operands regardless of whether the left operand already determines the result.

In Java, & and | evaluate both boolean operands, unlike && and ||. A mistaken operator may compile successfully yet cause unnecessary work, runtime exceptions, or different grouping because non-short-circuit operators have different precedence. section 2.7

A bounds-check typo

In `index >= 0 && index < data.length & data[index] > 0`, the final operand is still evaluated after the bounds condition is false. An out-of-range index can therefore produce ArrayIndexOutOfBoundsException. Use `&&` for dependent checks. section 2.7

Avoid accidental eager evaluation by:

  • Using && or || when later operands should run only when necessary. section 2.7
  • Adding parentheses or separate if statements when mixing operators with different precedence. section 2.7

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains evaluation behavior, exceptions, compound assignments, and precedence, but does not develop implementation internals.
Examples
Uses an array bounds check, compound boolean checks, complex type tests, and explicit conditional rewrites.
What it assumes
Assumes familiarity with Java boolean expressions, conditionals, and operator precedence.
What you can do after
Diagnose/debug with it by identifying eager operand evaluation, unsafe dependent checks, redundant computation, and precedence changes.
How it connects
Connects non-short-circuit operators with short-circuit operators, compound assignment, and operator precedence.

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.