Java : Boolean with Examples

Boolean (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Boolean methods.


Summary

The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field whose type is boolean.

Class diagram

// Compile error.
final var list = new ArrayList<boolean>();
// OK.
final var list = new ArrayList<Boolean>();
list.add(true);
list.add(false);
list.add(true);

System.out.println(list); // [true, false, true]

Fields

static final Boolean FALSE

The Boolean object corresponding to the primitive value false.

System.out.println(Boolean.FALSE); // false

static final Boolean TRUE

The Boolean object corresponding to the primitive value true.

System.out.println(Boolean.TRUE); // true

static final Class<Boolean> TYPE

The Class object representing the primitive type boolean.

System.out.println(Boolean.TYPE.getSimpleName()); // "boolean"
System.out.println(Boolean.TYPE.isPrimitive()); // true

Constructors

Boolean (boolean value)

Deprecated, for removal: This API element is subject to removal in a future version. It is rarely appropriate to use this constructor.

Deprecated.

Boolean (String s)

Deprecated, for removal: This API element is subject to removal in a future version. It is rarely appropriate to use this constructor.

Deprecated.

Methods

boolean booleanValue ()

Returns the value of this Boolean object as a boolean primitive.

final var value = Boolean.valueOf(true).booleanValue();
System.out.println(value); // true
final var value = Boolean.valueOf(false).booleanValue();
System.out.println(value); // false

static int compare (boolean x, boolean y)

Compares two boolean values.

System.out.println(Boolean.compare(true, true)); // 0
System.out.println(Boolean.compare(true, false)); // 1
System.out.println(Boolean.compare(false, true)); // -1
System.out.println(Boolean.compare(false, false)); // 0

int compareTo (Boolean b)

Compares this Boolean instance with another.

final var value1 = Boolean.valueOf(true);
final var value2 = Boolean.valueOf(true);
System.out.println(value1.compareTo(value2)); // 0
final var value1 = Boolean.valueOf(true);
final var value2 = Boolean.valueOf(false);
System.out.println(value1.compareTo(value2)); // 1
final var value1 = Boolean.valueOf(false);
final var value2 = Boolean.valueOf(true);
System.out.println(value1.compareTo(value2)); // -1
final var value1 = Boolean.valueOf(false);
final var value2 = Boolean.valueOf(false);
System.out.println(value1.compareTo(value2)); // 0

Optional<DynamicConstantDesc<Boolean>> describeConstable ()

Returns an Optional containing the nominal descriptor for this instance.

final var ret = Boolean.valueOf(true).describeConstable();

// Optional[DynamicConstantDesc[ConstantBootstraps::getStaticFinal(TRUE/ClassDesc[Boolean])Boolean]]
System.out.println(ret);

boolean equals (Object obj)

Returns true if and only if the argument is not null and is a Boolean object that represents the same boolean value as this object.

final var value1 = Boolean.valueOf(true);

System.out.println(value1.equals(true)); // true
System.out.println(value1.equals(false)); // false

final var value2 = Boolean.valueOf(false);

System.out.println(value1.equals(value1)); // true
System.out.println(value1.equals(value2)); // false

System.out.println(value1.equals(1)); // false
System.out.println(value1.equals("true")); // false

static boolean getBoolean (String name)

Returns true if and only if the system property named by the argument exists and is equal to, ignoring case, the string "true".

System.setProperty("abc", "true");
System.setProperty("xyz", "false");

System.out.println(Boolean.getBoolean("abc")); // true
System.out.println(Boolean.getBoolean("xyz")); // false

int hashCode ()

Returns a hash code for this Boolean object.

System.out.println(Boolean.valueOf(true).hashCode()); // 1231
System.out.println(Boolean.valueOf(false).hashCode()); // 1237

static int hashCode (boolean value)

Returns a hash code for a boolean value; compatible with Boolean.hashCode().

System.out.println(Boolean.hashCode(true)); // 1231
System.out.println(Boolean.hashCode(false)); // 1237

static boolean logicalAnd (boolean a, boolean b)

Returns the result of applying the logical AND operator to the specified boolean operands.

System.out.println(Boolean.logicalAnd(true, true)); // true
System.out.println(Boolean.logicalAnd(true, false)); // false
System.out.println(Boolean.logicalAnd(false, true)); // false
System.out.println(Boolean.logicalAnd(false, false)); // false

static boolean logicalOr (boolean a, boolean b)

Returns the result of applying the logical OR operator to the specified boolean operands.

System.out.println(Boolean.logicalOr(true, true)); // true
System.out.println(Boolean.logicalOr(true, false)); // true
System.out.println(Boolean.logicalOr(false, true)); // true
System.out.println(Boolean.logicalOr(false, false)); // false

static boolean logicalXor (boolean a, boolean b)

Returns the result of applying the logical XOR operator to the specified boolean operands.

System.out.println(Boolean.logicalXor(true, true)); // false
System.out.println(Boolean.logicalXor(true, false)); // true
System.out.println(Boolean.logicalXor(false, true)); // true
System.out.println(Boolean.logicalXor(false, false)); // false

static boolean parseBoolean (String s)

Parses the string argument as a boolean.

System.out.println(Boolean.parseBoolean("true")); // true
System.out.println(Boolean.parseBoolean("false")); // false

System.out.println(Boolean.parseBoolean("True")); // true
System.out.println(Boolean.parseBoolean("False")); // false

System.out.println(Boolean.parseBoolean("yes")); // false
System.out.println(Boolean.parseBoolean("abcd")); // false

System.out.println(Boolean.parseBoolean(null)); // false

String toString ()

Returns a String object representing this Boolean's value.

final var ret1 = Boolean.valueOf(true).toString();
System.out.println(ret1); // "true"

final var ret2 = Boolean.valueOf(false).toString();
System.out.println(ret2); // "false"

static String toString (boolean b)

Returns a String object representing the specified boolean.

final var ret1 = Boolean.toString(true);
System.out.println(ret1); // "true"

final var ret2 = Boolean.toString(false);
System.out.println(ret2); // "false"

static Boolean valueOf (boolean b)

Returns a Boolean instance representing the specified boolean value.

System.out.println(Boolean.valueOf(true)); // true
System.out.println(Boolean.valueOf(false)); // false

static Boolean valueOf (String s)

Returns a Boolean with a value represented by the specified string.

System.out.println(Boolean.valueOf("true")); // true
System.out.println(Boolean.valueOf("false")); // false

System.out.println(Boolean.valueOf("True")); // true
System.out.println(Boolean.valueOf("False")); // false

System.out.println(Boolean.valueOf("yes")); // false
System.out.println(Boolean.valueOf("abcd")); // false

System.out.println(Boolean.valueOf(null)); // false

Related posts

To top of page