liveBook · Manning

Logical OR

As explained in 100 Java Mistakes and How to Avoid Them

The logical || operator evaluates boolean operands and produces true when at least one operand is true.

Logical OR (||) expresses alternative conditions: a combined condition succeeds when either operand is true. Confusing it with logical AND (&&) can make validation or filtering conditions impossible to satisfy. section 2.8

Avoid the common failure modes:

  • Use || when rejecting values outside a range; a value cannot be both below the lower bound and above the upper bound. section 2.8
  • When negating a disjunction, preserve De Morgan’s law: !(a || b) becomes !a && !b, not !a || !b. section 2.8
  • Prefer parentheses around compound conditions so the intended grouping remains visible. section 2.8

Correcting a printable-character check

The faulty condition uses && between mutually exclusive bounds, so it never rejects a character outside the printable range. Replace that inner operator with || while retaining the exclusions for tab and newline.

void processLine(String line) {
  // Ignore comments
  if (!line.startsWith("#") || !line.startsWith("//")) {
    processContentLine(line);
  }
}

Inspect the deceptive negation that lets every line reach content processing instead of ignoring comment lines. section 2.8

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains impossible conjunctions, corrected conditions, De Morgan’s laws, and practical consequences, but does not develop operator internals.
Examples
Uses byte-range validation, printable ASCII detection, and comment-line filtering as concrete failure cases.
What it assumes
Assumes familiarity with Java conditional expressions and Boolean logic rather than teaching those foundations from zero.
What you can do after
Diagnose impossible conditions, apply De Morgan’s laws, and choose || or && deliberately when correcting Java predicates.
How it connects
Connects logical OR with logical AND, Boolean logic, De Morgan’s laws, validation, filtering, and static analysis.

Also explained in

Compare how 6 books explain logical OR →

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.