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.
boolean flag = Boolean.parseBoolean("TRUE"); // true Boolean maybe = null; // "not set"
Try it
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.
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
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
parseBooleannever throws."yes","1","on"and typos are allfalse. If you need validation, check the text yourself.- Prefer primitives. Use
booleanfor fields and parameters unlessnullhas a meaning; aBooleanfield is a hidden third state that invitesNullPointerException. - Null-safe tests.
Boolean.TRUE.equals(x)istrueonly for a non-null true value;!Boolean.FALSE.equals(x)treatsnullas "not false". - Conditions unbox.
if (flag)with anullBooleanthrows. The ternary operator can also unbox unexpectedly, as incond ? someBoolean : false. new Boolean(...)is deprecated for removal. It creates a third, redundant object. UsevalueOfor autoboxing.&&versuslogicalAnd. The operator short-circuits; the method call evaluates both arguments first, so it cannot guard against side effects.- Sorting.
falsesorts beforetrue.Comparator.comparing(Item::isActive)therefore lists inactive items first. - Memory.
boolean[]stores one byte per element in most JVMs. For large flag sets useBitSet. - Thread safety. The class is immutable. A shared
booleanflag between threads must still bevolatileor anAtomicBoolean.