liveBook · Manning

Boolean

As explained in 100 Java Mistakes and How to Avoid Them

Boolean is the Java wrapper class for the primitive boolean type, with TRUE and FALSE instances and the possibility of a null reference.

Boolean wraps the primitive boolean type and provides Boolean.TRUE and Boolean.FALSE. Using null to represent a third state turns a simple two-value choice into an ambiguous three-value API. section 5.1.1

The nullable Boolean trap

A dialog might return true for Yes, false for No, and null for Cancel. Code such as if (shouldMoveToWishList()) can mishandle that third outcome, especially when unboxing occurs. Replace the nullable Boolean with named enum values such as YES, NO, and CANCEL, or model the operation directly with REMOVE_FROM_BASKET, MOVE_TO_WISH_LIST, and CANCEL.

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains Boolean's two values, the ambiguity introduced by null, and a domain-specific enum replacement.
Examples
Demonstrates a shopping-basket dialog whose Yes, No, and Cancel outcomes expose the nullable Boolean problem.
What you can do after
Evaluate Boolean APIs for ambiguous null states and replace them with explicitly named enum outcomes.
How it connects
Connects Boolean with primitive boolean, null references, wrapper types, and enums.

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.