java.lang.Boolean

The Boolean class simply a wrapper class for the primitive type boolean. It wraps the boolean primitive value to an object. An object of type Boolean contains a single field whose type is boolean. So what would be the benefit if wraps around the primitive data type to an object? Well basically we would be able to access all the methods that is readily available on the Boolean class which is basically useful. In addition, this class provides many methods for converting a boolean to a String and a String to a boolean, as well as other constants and methods useful when dealing with a boolean. We will go over each and every method available to Boolean class thoroughly.

Boolean Class Syntax

public final class Boolean
extends Object
implements Serializable, Comparable<Boolean>

Boolean Compatibility Version

Boolean Class is available since JDK 1.0

Boolean Basic Usage

The Boolean class as part of the java.lang package is one of the classes of the java api that is being widely used. Let’s tackle the basics of using the Boolean class. Let’s discuss first how to instantiate a Boolean object

Boolean value = new Boolean(true);

Basically the declaration of a Boolean object is almost the same as we are doing in assigning primitive data type boolean. Similarly to boolean primitive the Boolean object has only two possible values, which is either true or false. This is a straightforward method to instantiate a Boolean object type. Let’s take another way to do this.

Boolean value = true;

The 2nd method is using the autoboxing concept of java programming language which directly converts a primitive data type to its corresponding wrapper class.  If you would like to go more into details, I am recommending for you to read the official java documentation regarding autoboxing and unboxing.

There are two constructor available to Boolean class which we can use to instantiate a Boolean object:

Constructor Description
Boolean(boolean value) Allocates a Boolean object representing the value argument.
Boolean(String s) Allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string “true”.

 

We already discussed on how to use the Boolean(boolean value) in instantiating a Float object. So basically the other constructors usage is almost the same only in the other case, the input is in String format.

Boolean Test of Equality

Normally when we do equality test for boolean primitive data type we do like this:

		boolean b1= true;
		boolea b2 = false;
		if(b1==b2){
			System.out.println("They are equal");
		}
		else{
			System.out.println("Values are not equal");
		}

This will no longer work in dealing with Boolean object type. To test equality we need to make use of equals which is a method inherited from Object class.

		Boolean val1 = true;
		Boolean val2 = new Float(true);
		if(val1.equals(val2)){
			System.out.println("They are equal");
		}
		else{
			System.out.println("They are not equal");			
		}

Boolean Method Usage Examples

Modifier and Type Method and Description
boolean booleanValue()
Returns the value of this Boolean object as a boolean primitive.
static int compare(boolean x, boolean y)
Compares two boolean values.
int compareTo(Boolean b)
Compares this Boolean instance with another.
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.
static boolean getBoolean(String name)
Returns true if and only if the system property named by the argument exists and is equal to the string “true”.
int hashCode()
Returns a hash code for this Boolean object.
static int hashCode(boolean value)
Returns a hash code for a boolean value; compatible with Boolean.hashCode().
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.