Boolean Class in JAVA with Example
Boolean Class is a wrapper class that wraps the primitive type boolean. This class provides mechanism to convert primitive type “boolean” to object type “Boolean” or vice versa.
Object of Class Boolean can contain only single value whose type is boolean.
This class extends Object class and implements Serializable and Comparable interfaces.
Table of Contents
Boolean Class Constructors
1. Boolean (boolean value)
This constructor creates a Boolean object with value which is of type boolean that can be true or false
2. Boolean (String string)
This constructor creates a Boolean object with value “true” if and only if value of string passed is “true” ignoring case or else object is initialized with “false”.
Boolean Methods
1. boolean booleanValue()
This method returns primitive value of the Boolean object.
Let us discuss this method with the help of example as shown below
Output:
Description:
Above program has been divided into 3 steps that we will discuss one by one
- In Step 1, Boolean class object is created and value “true” is assigned to it
- In Step 2, primitive type of boolean is created
- In Step 3, value of Boolean object is assigned to primitive type boolean using booleanValue() method.
2. int compareTo(Boolean b)
This method as the name suggests compares one Boolean object with other. This method returns int value which can be negative, positive or zero.
Zero is returned when the two objects have same values.
Positive value is returned when calling object has value “true” and argument object has value “false”.
Negative value is returned when calling object has value “false” and argument object has value “true”.
Let us discuss this method with the help of example as shown below
Output:
Description:
Above program has been divided into 3 steps that we will discuss one by one
- In Step 1, Boolean class objects are created.
- In Step 2, Values are assigned to the objects created in step 1.
- In Step 3, Boolean objects are compared using method compareTo()
3. boolean equals(Object obj)
This method returns boolean value true or false, true when the both the Boolean objects have same value, or else false.
Let us discuss this method with the help of example as shown below
Output:
Description:
Above program has been divided into 3 steps that we will discuss one by one
- In Step 1, Boolean class objects are created and values are assigned to them.
- In Step 2, boolean flag1 and flag2 are created.
- In Step 3, objects are compared using equals() method and boolean values are stored in the flag.
4. static boolean getBoolean(String name)
This method is to check boolean value of System property. This method will return true if the system property named by the argument exists and has value ”true” assigned to it or else this method will return false.
Let us discuss this method with the help of example as shown below
Output:
Description:
Above program has been divided into 3 steps that we will discuss one by one
- In Step 1, boolean primitives are created.
- In Step 2, Values for two system properties are assigned using setProperty () method.
- In Step 3, System properties are assigned to boolean primitive types using getBoolean() method.
5. int hashCode()
This method returns a hash code for the calling Boolean object.
Let us discuss this method with the help of example as shown below
Output:
Description:
Above program has been divided into 2 steps that we will discuss one by one
- In Step 1, Boolean objects are created and values are assigned to them.
- In Step 2, Hash codes are obtained using hashCode() method.
6. static boolean parseBoolean(String s)
This method parses the value of string arguments, and will return true if and only if string value is “true” (ignoring case) or else it will return false.
Let us discuss this method with the help of example as shown below
Output:
Description:
Above program has been divided into 3 steps that we will discuss one by one
- In Step 1, two strings are created and values are assigned to them.
- In Step 2, boolean types are created.
- In Step 3, Values are assigned to primitive types using parseBoolean() method.
7. String toString()
This method returns a String object which represents the value of calling Boolean object. If the value of Boolean object is null then this method will return false.
Let us discuss this method with the help of example as shown below
Output:
Description:
Above program has been divided into 3 steps that we will discuss one by one
- In Step 1, Boolean class objects are created
- In Step 2, Values are assigned to objects created in Step 1.
- In Step 3, String values of Boolean objects are assigned to String objects using toString() method
8. static String toString(boolean b)
This method returns a String object which represents the value of calling boolean primitive type. This method is called directly from the Boolean class as this method is static.
Let us discuss this method with the help of example as shown below
Output:
Description:
Above program has been divided into 3 steps that we will discuss one by one
- In Step 1, boolean primitive types are created.
- In Step 2, Values are assigned to primitive types created in Step 1.
- In Step 3, String values of boolean primitives are assigned to String objects using toString() method.
9. static Boolean valueOf(boolean b)
This method returns an object of Boolean class that represents the specified boolean value.
Let us discuss this method with the help of example as shown below
Output:
Description:
Above program has been divided into 3 steps that we will discuss one by one
- In Step 1, Boolean objects are created.
- In Step 2, boolean primitives are created and values are assigned to them
- In Step 3, Values of primitive types are assigned to Boolean objects
10. static Boolean valueOf(String s)
This method returns Boolean object with a value true or false
true when the value of string is “true” (ignoring case)
false when the value is any string other than “true”.
Let us discuss this method with the help of example as shown below
Output:
Description:
Above program has been divided into 3 steps that we will discuss one by one
- In Step 1, Boolean objects are created.
- In Step 2, String objects are created and values are assigned to them
- In Step 3, boolean value of Strings are assigned to Boolean objects
Источник