Java : Boolean with Examples
Boolean (Java SE 18 & JDK 18) API Examples.
You will find code examples on most Boolean methods.
Summary
// 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
System.out.println(Boolean.FALSE); // false
static final Boolean TRUE
System.out.println(Boolean.TRUE); // true
static final Class<Boolean> TYPE
System.out.println(Boolean.TYPE.getSimpleName()); // "boolean"
System.out.println(Boolean.TYPE.isPrimitive()); // true
Constructors
Boolean (boolean value)
Deprecated.
Boolean (String s)
Deprecated.
Methods
boolean booleanValue ()
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)
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)
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 ()
final var ret = Boolean.valueOf(true).describeConstable();
// Optional[DynamicConstantDesc[ConstantBootstraps::getStaticFinal(TRUE/ClassDesc[Boolean])Boolean]]
System.out.println(ret);
boolean equals (Object obj)
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)
System.setProperty("abc", "true");
System.setProperty("xyz", "false");
System.out.println(Boolean.getBoolean("abc")); // true
System.out.println(Boolean.getBoolean("xyz")); // false
int hashCode ()
System.out.println(Boolean.valueOf(true).hashCode()); // 1231
System.out.println(Boolean.valueOf(false).hashCode()); // 1237
static int hashCode (boolean value)
System.out.println(Boolean.hashCode(true)); // 1231
System.out.println(Boolean.hashCode(false)); // 1237
static boolean logicalAnd (boolean a, boolean b)
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)
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)
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)
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 ()
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)
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)
System.out.println(Boolean.valueOf(true)); // true
System.out.println(Boolean.valueOf(false)); // false
static Boolean valueOf (String s)
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