Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The Boolean class

Reference for java.lang.Boolean: the boolean wrapper, parseBoolean rules, logical helpers, TRUE and FALSE constants, null pitfalls and Boolean in collections.

Boolean is the wrapper class for the primitive boolean. A Boolean object holds exactly one of two values, true or false, and the class has only two instances in normal use: Boolean.TRUE and Boolean.FALSE. Autoboxing and Boolean.valueOf() always return one of those two, so even == works between boxed booleans (though equals is still the better habit).

You need the wrapper whenever a boolean must be an object: in a List<Boolean>, as a Map value, or in an API where "unknown" must be represented by null as a third state. The static helpers parse text and apply logical operators in a form that can be used as method references.

Java
boolean flag = Boolean.parseBoolean("TRUE");   // true
Boolean maybe = null;                          // "not set"

Try it

Java Parsing and logical helpers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Three states and null

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public final class Boolean implements Comparable<Boolean>, java.io.Serializable
Member Description
TRUE, FALSE The two shared Boolean objects.
valueOf(boolean), valueOf(String) Returns TRUE or FALSE.
parseBoolean(String) true only if the text equals "true" ignoring case; everything else, including null, is false.
booleanValue() Unboxes to boolean.
toString(), toString(boolean) "true" or "false".
logicalAnd, logicalOr, logicalXor Functional forms of &, | and ^.
compare(boolean, boolean), compareTo(Boolean) Ordering, with false < true.
getBoolean(String) Reads a system property as a boolean (rarely used).

Throws: nothing in the static methods. Unboxing a null Boolean throws NullPointerException.

Usage notes

  • parseBoolean never throws. "yes", "1", "on" and typos are all false. If you need validation, check the text yourself.
  • Prefer primitives. Use boolean for fields and parameters unless null has a meaning; a Boolean field is a hidden third state that invites NullPointerException.
  • Null-safe tests. Boolean.TRUE.equals(x) is true only for a non-null true value; !Boolean.FALSE.equals(x) treats null as "not false".
  • Conditions unbox. if (flag) with a null Boolean throws. The ternary operator can also unbox unexpectedly, as in cond ? someBoolean : false.
  • new Boolean(...) is deprecated for removal. It creates a third, redundant object. Use valueOf or autoboxing.
  • && versus logicalAnd. The operator short-circuits; the method call evaluates both arguments first, so it cannot guard against side effects.
  • Sorting. false sorts before true. Comparator.comparing(Item::isActive) therefore lists inactive items first.
  • Memory. boolean[] stores one byte per element in most JVMs. For large flag sets use BitSet.
  • Thread safety. The class is immutable. A shared boolean flag between threads must still be volatile or an AtomicBoolean.