Class Boolean in Java
This class allows the coder to define primitive type Boolean for an object. An object of type boolean can contain a single field whose type is boolean. The Boolean class provides an immutable object wrapper for a boolean value. It is very useful when we need to treat the boolean value as an object.
The Boolean class supports to implement Reflection API and class literals. Note that TRUE and FALSE constants are Boolean objects; they are not the same as true and false boolean values. booleanValue() returns the boolean value of a Boolean object.
The class method getBoolean() retrieves the boolean value of a named property from the system property list. The class method valueOf() parses a string and returns the Boolean value it represents.
This is useful when we need to treat a boolean value as an object. For example, there are a number of utility methods that take a reference to an Object as one of their arguments. We cannot specify a boolean value for one of these arguments, but we can provide a reference to a Boolean object that encapsulates the boolean value.
Below is the Structure of Boolean class:
public final class java.lang.Booean extends java.lang.Object implements java.io.Serializable
{
//Member elements of the Boolean class
public final static Boolean FALSE;
//A constant Boolean object whose's value is FALSE
public final static Boolean TRUE;
//A constant Boolean object whose's value is TRUE
public final static Class TYPE;
//The Class object that represents the type boolean. It is always true if Boolean.TYPE==boolean.class.
//constructors:
public Boolean(boolean value);// creates a Boolean object initialized with the value provided as an argument. So converts a boolean value to the Boolean object.
public Boolean(String Str);
//Creates a Boolean object initialize with the value TRUE if the String argument provided is not null and is equals to ignore case to the String //"True", Otherwise creates a boolean object initialized as false.
//Methods:
public boolean booleanValue();//This method returns the boolean value of an object contained.
public boolean equals(Object obj);//This method returns true if the obj is an instance of Boolean and it contains the same value as the object this method is associated with. it returns false if they do not contain the same value.
public static boolean getBoolean(Sting str);//this method returns the boolean value of the named system property.
public int hashcode();//Returns the hashcode based on the boolean value of the object.
public String toString();//This method returns "true" if the value of the object is true. Otherwise, it returns false. Basically a string representation of the object.
public static Boolean valueOf(String s);//This method returns a Boolean object with the value TRUE if the String argument provided is not null and is equals to ignore case to the String //"True", Otherwise returns a boolean object whose value is false.
}
Methods | Description |
booleanValue() | Returns: The primitive boolean value of this object. |
equals(Object obj) | The result is true if and only if the argument is not null and is a Boolean object that contains the same boolean value as this object.
Returns: True if the objects are the same; false otherwise. |
getBoolean(String name) | The result is true if and only if the system property named by the argument exists and is equal, ignoring case to the string true. |
hashCode() | Returns: a hash code value for this object. |
toString() | If this object contains the value true, a string equal to “true” is returned. Otherwise, a string equal to “false” is returned. |
valueOf(String s) | A new Boolean object is constructed. This Boolean contains the value true if the string argument is not null and is equal, ignoring case, to the string “true”.
Returns: the Boolean value represented by the string. |
Apart from these this class also has the below-inherited methods from Class Object :
From Object class
- clone()
- getClass()
- equals(Object obj);
- finalize()
- notify()
- notifyAll()
- wait()
- wait(long timeput)
- wait(long timeout,int nanos)