Boolean Class in Java
Boolean class in Java is a wrapper class that wraps (converts) a primitive “boolean” data type value in an object.
An object of Boolean class contains a single boolean type field where we can store a primitive boolean value.
Boolean class was introduced in JDK 1.0. It is present in java.lang.Boolean package.
Java Boolean Class Declaration
The general declaration of Boolean class in Java is as follows:
public final class Boolean
extends Number
implements Comparable<Boolean>
Boolean class extends Number class and implements Serializable and Comparable<Boolean> interfaces. Number class extends Object class.
Constructors
Boolean class in Java defines two constructors that are as:
1. Boolean(boolean value): This form of constructor accepts a boolean value as its parameter and converts it into a Boolean class object. The general syntax to create Boolean class object to wrap boolean value is as:
boolean value = true;
Boolean obj = new Boolean(value);
Here, we are converting a primitive boolean value into Boolean object. This is called boxing in java.
2. Boolean(String str): This constructor accepts a parameter of String type and converts that string into a Boolean object. The string contains a boolean value.
The general syntax to create a Boolean object by wrapping a string that contains a boolean value, as:
String str = "true";
Boolean b = new Boolean(str);
Boolean Class Methods in Java
In addition to methods inherited from the Number class and Object class, Java Boolean class also includes some useful important methods. They are as follows:
1. int compareTo(Boolean obj): This method compares the values of two Boolean class objects numerically. We call it using the below syntax:
int x = obj1.compareTo(obj2);
where, obj1 and obj2 are Boolean class objects.
- If obj1 == obj2, this method returns 0.
- If obj1 < obj2, it returns a negative value.
- If obj1 > obj2, it returns a positive value.
2. boolean equals(Object obj): This method compares a Boolean object with another specified Boolean object obj. If both have the same content, the method returns true, otherwise returns false.
3. static boolean parseBoolean(String str): This method returns the boolean value contained in the string argument str.
4. String toString(): The toString() method converts Boolean object into String object and returns that String object. It returns a string form of Boolean object.
5. static Boolean valueOf(String str): It converts string str containing some boolean value into Boolean object and returns that Boolean object.
In other words, it returns a Boolean object holding a value given by the specified string.
6. static Boolean valueOf(boolean b): This method converts the primitive boolean value into a Boolean object. In other words, it returns a Boolean object representing the specified boolean value.
7. static int compare(boolean x, boolean y): It is used to compare two boolean values numerically.
8. int hashCode(): It returns a hash code value for the Boolean object.
9. boolean booleanValue(): This method returns a boolean value of this Boolean object.
10. boolean logicalAnd(boolean a, boolean b): This method returns the result of applying the logical AND operator to the specified boolean operands a and b.
11. boolean logicalOr(boolean a, boolean b): This method returns the result of applying the logical OR operator to the specified boolean operands a and b.
12. boolean logicalXor(boolean a, boolean b): This method returns the result of applying the logical XOR operator to the specified boolean operands a and b.
13. int hashCode(boolean value): It returns a hash code for a boolean value that is compatible with Boolean.hashCode().
Boolean Class Methods Examples
Example 1: Let’s write a Java program in which we will use the method booleanValue() of Boolean class.
package javaProgram;
public class BooleanTest {
public static void main(String[] args)
{
Boolean b = new Boolean(true);
boolean value = b.booleanValue();
System.out.println(value);
}
}
Output: true
Example 2: Let’s create a Java program to compare two boolean values numerically using compare() method of Boolean class.
package javaProgram;
public class BooleanTest {
public static void main(String[] args)
{
// Initializing two boolean values to be compared.
Boolean b1 = true;
Boolean b2 = false;
// Call compare() method to compare two boolean values.
if (Boolean.compare(b1, b2) == 0) {
System.out.println("Both values are equal");
}
else if (Boolean.compare(b1, b2) > 0) {
System.out.println("b1 value is true");
}
else {
System.out.println("b2 value is true");
}
}
}
Output: b1 value is true
Example 3: Let’s create a Java program to compare two different Boolean objects numerically using compareTo() method of Boolean class.
package javaProgram;
public class BooleanTest {
public static void main(String[] args)
{
// Creating two objects of Boolean class and passing two boolean values.
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(false);
// Calling compareTo() method to compare two Boolean objects.
int compareValue = b1.compareTo(b2);
if (compareValue == 0)
System.out.println("b1 and b2 are equal");
else if (compareValue < 0)
System.out.println("b1 is less than b2");
else
System.out.println("b1 is greater than b2");
}
}
Output: b1 is greater than b2
Example 4: Let’s create a Java program to compare a Boolean object with another specified Boolean object using equals() method of Boolean class.
package javaProgram;
public class BooleanTest {
public static void main(String[] args)
{
// When two Boolean objects are different.
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(false);
if (b1.equals(b2))
System.out.println("b1 is equal to b2");
else
System.out.println("b1 is not equal to b2");
// When two Boolean objects are equal
b1 = new Boolean(true);
b2 = new Boolean(true);
if(b1.equals(b2))
System.out.print("b1 is equal to b2");
else
System.out.print("b1 is not equal to b2");
}
}
Output: b1 is not equal to b2 b1 is equal to b2
Example 5: Let us write Java code to convert a string value into a primitive boolean value using parseBoolean() method of Boolean class.
package javaProgram;
public class BooleanTest {
public static void main(String[] args)
{
String s = "true";
String str = "Yes";
boolean b1 = Boolean.parseBoolean(s);
boolean b2 = Boolean.parseBoolean(str);
System.out.println("Primitive boolean value = " +b1);
System.out.println("Primitive boolean value = " +b2);
}
}
Output: Primitive boolean value = true Primitive boolean value = false
Example 6: Let us write a Java program to convert a Boolean object into String object and store the returned String object into variable str of String type.
package javaProgram;
public class BooleanTest {
public static void main(String[] args)
{
Boolean b = true;
Boolean obj = new Boolean(b);
// Call to String() method to convert Boolean object into String object.
String str = obj.toString();
System.out.println("String representation: " +str);
System.out.println(str + false);
}
}
Output: String representation: true truefalse
Example 7: Let us write a Java program to convert a string containing a boolean value into Boolean object using valueOf() method of Boolean class.
package javaProgram;
public class BooleanTest {
public static void main(String[] args)
{
String str = "true";
Boolean obj = new Boolean(str);
Boolean b = obj.valueOf(str);
System.out.println("Boolean value: " +b);
}
}
Output: Boolean value: true
Example 8: Let’s make a Java program in which we will apply logical AND operator to the specified boolean values.
package javaProgram;
public class BooleanTest {
public static void main(String[] args)
{
Boolean b1 = true;
Boolean b2 = true;
// logicalAnd() method returns true if both boolean values are true.
Boolean b3 = Boolean.logicalAnd(b1,b2);
System.out.println("If b1 and b2 are true: "+b3);
Boolean b4 = true;
Boolean b5 = false;
// logicalAnd() method returns false if either one of the boolean values is false.
Boolean b6 = Boolean.logicalAnd(b4,b5);
System.out.println("If b1 is true and b2 is false: "+b6);
}
}
Output: If b1 and b2 are true: true If b1 is true and b2 is false: false
Example 9: Let’s make a Java program in which we will apply the logical OR operator to the specified boolean values.
package javaProgram;
public class BooleanTest {
public static void main(String[] args)
{
Boolean b1 = true;
Boolean b2 = true;
// logicalOr() method returns true if both boolean values are true.
Boolean b3 = Boolean.logicalOr(b1,b2);
System.out.println("If b1 and b2 are true: "+b3);
Boolean b4 = true;
Boolean b5 = false;
// logicalOr() method returns true if either one of the boolean values is false.
Boolean b6 = Boolean.logicalOr(b4,b5);
System.out.println("If b1 is true and b2 is false: "+b6);
}
}
Output: If b1 and b2 are true: true If b1 is true and b2 is false: true
Example 10: Let’s create a Java program in which we will apply the logical XOR operator to the specified boolean values.
package javaProgram;
public class BooleanTest {
public static void main(String[] args)
{
Boolean b1 = true;
Boolean b2 = true;
// logicalXor() method returns false if both boolean values are true.
Boolean b3 = Boolean.logicalXor(b1,b2);
System.out.println("If b1 and b2 are true: "+b3);
Boolean b4 = true;
Boolean b5 = false;
// logicalXor() method returns true if either one of the boolean values is false.
Boolean b6 = Boolean.logicalXor(b4,b5);
System.out.println("If b1 is true and b2 is false: "+b6);
}
}
Output: If b1 and b2 are true: false If b1 is true and b2 is false: true
In this tutorial, you learned a wrapper Boolean class in Java with some important example programs. Hope that you will have understood the basic concept of Boolean class object and practiced all programs.
Thanks for reading!!!