Understanding the Java Boolean Class
The Java Boolean class wraps the primitive boolean type into an object. It allows for the creation of Boolean objects that can hold a true or false value, which is useful when dealing with collections that cannot store primitive data types directly.
Java Boolean Class
The Java 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.
Boolean Class Declaration
The declaration for java.lang.Boolean
class:
public final class Boolean
extends Object
implements Serializable, Comparable
Boolean Class Fields
static Boolean FALSE
− The Boolean object corresponding to the primitive value false.static Boolean TRUE
− The Boolean object corresponding to the primitive value true.static Class<Boolean> TYPE
− The Class object representing the primitive type boolean.
Boolean Class Constructors
Constructor | Description |
---|---|
Boolean(boolean value) |
Allocates a Boolean object representing the value argument. |
Boolean(String s) |
Allocates a Boolean object representing true if the string argument is "true" (case ignored). |
Boolean Class Methods
Method | Description |
---|---|
boolean booleanValue() |
Returns the value as a boolean primitive. |
int compareTo(Boolean b) |
Compares this Boolean instance with another. |
boolean equals(Object obj) |
Returns true if this object represents the same boolean value as the argument. |
static boolean getBoolean(String name) |
Returns true if the system property named by the argument is equal to "true". |
int hashCode() |
Returns a hash code for this Boolean object. |
int hashCode(boolean value) |
Returns a hash code for a given boolean value. |
static boolean logicalAnd(boolean a, boolean b) |
Returns the result of applying the logical AND operator to the specified boolean operands. |
static boolean logicalOr(boolean a, boolean b) |
Returns the result of applying the logical OR operator to the specified boolean operands. |
static boolean logicalXor(boolean a, boolean b) |
Returns the result of applying the logical XOR operator to the specified boolean operands. |
static boolean parseBoolean(String s) |
Parses the string argument as a boolean. |
String toString() |
Returns a String object representing this Boolean's value. |
static String toString(boolean b) |
Returns a String object representing the specified boolean. |
static Boolean valueOf(boolean b) |
Returns a Boolean instance representing the specified boolean value. |
static Boolean valueOf(String s) |
Returns a Boolean with a value represented by the specified string. |
Example of Java Boolean Class
The following example shows the usage of some important methods provided by the Boolean class:
package com.tutorialsarena;
public class BooleanDemo {
public static void main(String[] args) {
Boolean b1, b2;
b1 = Boolean.valueOf(true);
b2 = Boolean.valueOf(false);
int res = b1.compareTo(b2);
if (res == 0) {
System.out.println("Both values are equal");
} else if (res > 0) {
System.out.println("Object value is true");
} else {
System.out.println("Argument value is true");
}
}
}
Object value is true