A null check tests whether a reference contains null, but operator precedence can make the check test the wrong expression. In `"Value: " + value != null ? value : "(unknown)"`, concatenation happens first, so the condition is always true and null may be returned unchanged. section 2.2.3
A check defeated by concatenation
The faulty expression is parsed as `(("Value: " + value) != null) ? value : "(unknown)"`. Split the computation into steps: first select `value != null ? value : "(unknown)"`, then concatenate it. Parentheses also make the intended grouping explicit. section 2.2.3
Reduce null-related failures with targeted practices:
- Separate expressions that combine concatenation, arithmetic, and conditional operators; for display text, use a temporary variable or a formatting call. Java 15 also provides `formatted`, while `requireNonNullElse` can supply a fallback. section 2.2.3
- Avoid null values where possible, use `Optional` when absence is meaningful, add nullability annotations, and use interprocedural static analysis for potential `NullPointerException` problems. section 5.1
- In concurrent queues, replace an `isEmpty()` check followed by `remove()` with `poll()` and check the returned local value; use atomic map operations such as `merge()` for related get-and-put patterns. section 9.12
String format(String value) {
return "Value: " + value != null ? value : "(unknown)";
}
Inspect the compact faulty expression, then compare its precedence-expanded form with the separated corrected version. section 2.2.3

