Signup/Sign In

Java Boolean class

The Boolean class is wrapper in class that is used to wrap a value of the primitive type boolean in an object. An object of type Boolean contains a single field whose type is boolean.

This class consists of many helping methods for converting a boolean to a String and a String to a boolean, as well as other constants and methods helpful while working with boolean.

This class is located into java.lang package and has following declaration.

Declaration

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

Here we are explaining various methods of Boolean class and their example.

1. parseBoolean(String s)

The parseBoolean() method is used to parse the string argument as a boolean. It returns a boolean value either true or false. If the string argument is not null and is equal, ignoring case, to the string "true", it returns true.

It takes a string argument that contains the boolean representation to be parsed.

Syntax :

	
public static boolean parseBoolean(String s)
	

Example:

In this example, we are parsing string arguments to the boolean type using the parseBoolean() method. This method returns true if the specified string can be parsed to boolean, false otherwise.

	
public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        boolean obj1 = Boolean.parseBoolean("True"); 
    	boolean obj2 = Boolean.parseBoolean("False");
        boolean obj3 = Boolean.parseBoolean("StUdYtONiGhT"); 
        boolean obj4 = Boolean.parseBoolean("STUDYTONIGHT"); 
        boolean obj5 = Boolean.parseBoolean("studytonight"); 
        System.out.println(obj1); 
        System.out.println(obj2); 
        System.out.println(obj3); 
        System.out.println(obj4); 
        System.out.println(obj5);   
    } 
}
	

boolean-class-example

2. booleanValue()

The booleanValue() method is used to get boolean value of this Boolean object as a boolean primitive. It returns primitive type boolean value of the Boolean object. Declaration of this method is given below.

Syntax:

	
public boolean booleanValue()
	

Example:

We are using booleanValue() method to get boolean value of the Boolean object. See the below example.

	
public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        Boolean obj1 = new Boolean("True"); 
        Boolean obj2 = new Boolean("StUdYtONiGhT"); 
        Boolean obj3 = new Boolean("STUDYTONIGHT"); 
        Boolean obj4 = new Boolean("studytonight"); 

    	boolean obj5 = obj1.booleanValue();
    	boolean obj6 = obj2.booleanValue();
    	boolean obj7 = obj3.booleanValue();
    	boolean obj8 = obj4.booleanValue();

        System.out.println(obj5); 
        System.out.println(obj6); 
        System.out.println(obj7); 
        System.out.println(obj8);   
    } 
}
	

boolean-class-example-1.JPG

3. valueOf(boolean b)

The valueOf() method is used to get Boolean instance representing the specified boolean value. If the specified boolean value is true, this method returns Boolean. TRUE; if it is false, this method returns Boolean. FALSE.

This method takes a boolean type argument and returns a Boolean object representing to the specified argument.

Syntax:

	
public static boolean valueOf(boolean b)
	

Example:

In this example, we are parsing primitive type boolean value to Boolean object. We used valueOf() method to get boolean object.

	
public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
    	boolean obj1 = true;
    	boolean obj2 = false;
    	
    	Boolean obj3 = Boolean.valueOf(obj1);
    	Boolean obj4 = Boolean.valueOf(obj2);

        System.out.println(obj3); 
        System.out.println(obj4);    
    } 
}
	

boolean-class-example

4. valueOf(String s)

The valueOf() method is used to get a Boolean with a value represented by the specified string. It takes a string argument and returns a Boolean value represented by the specified argument.

Syntax:

	
public static boolean valueOf(String s)
	

Example:

	
public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        Boolean obj1 = Boolean.valueOf("True"); 
        Boolean obj2 = Boolean.valueOf(null);
        Boolean obj3 = Boolean.valueOf("StUdYtONiGhT"); 
        Boolean obj4 = Boolean.valueOf("STUDYTONIGHT"); 
        Boolean obj5 = Boolean.valueOf("studytonight"); 
        System.out.println(obj1); 
        System.out.println(obj2); 
        System.out.println(obj3); 
        System.out.println(obj4); 
        System.out.println(obj5);   
    } 
}
	

boolean-class-example-3.JPG

5. toString(boolean b)

This method is used to get a String object representing the specified boolean. If the specified boolean is true, then the returned string will be "true", otherwise the string "false" will be returned.

It takes a boolean argument and returns string representation of the specified boolean. Syntax of this method is given below.

Syntax:

	
public static String toString(boolean b)
	

Example:

We can use this method to get string representation of boolean type value. In this example, we are taking two boolean variables that hold boolean value and getting their string value using the toString() method. See the below example.

	
public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        boolean obj1 = true; 
        boolean obj2 = false; 

        String s1 = Boolean.toString(obj1); 
        String s2 = Boolean.toString(obj2); 

        System.out.println(s1); 
        System.out.println(s2);
    } 
}
	

boolean-class-example

6. toString()

This method returns a String object representing this Boolean's value. If this object represents the value true, a string equal to "true" is returned. Otherwise, a string equal to "false" is returned. It does not take any argument.

Syntax:

	
public String toString()
	

Example:

In this example, we are getting string representation of an Boolean object by using the toString() method.

	
public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        Boolean obj1 = new Boolean("True"); 
        Boolean obj2 = new Boolean(null);
        Boolean obj3 = new Boolean("StUdYtONiGhT"); 
        Boolean obj4 = new Boolean("STUDYTONIGHT"); 
        Boolean obj5 = new Boolean("studytonight"); 
	
    	String s1 = obj1.toString();
    	String s2 = obj2.toString();
    	String s3 = obj3.toString();
    	String s4 = obj4.toString();
    	String s5 = obj5.toString();

		System.out.println(s1); 
        System.out.println(s2); 
        System.out.println(s3); 
        System.out.println(s4); 
        System.out.println(s5); 
    } 
}
	

boolean-class-example

7. int hashCode()

This method is used to get a hash code for a boolean value. It returns a hash code value of an boolean object. It does not take any parameter but returns an int value that represents hash code of a boolean value.

This method was included in Java 8 version. Syntax of this method is given below.

Syntax:

	
public int hashCode()
	

Example:

Lets create an example to get hash code of a boolean value. Here we have created several boolean variables that we used to fetch different hash code value.

	
public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        Boolean obj1 = new Boolean("True"); 
        Boolean obj2 = new Boolean(null);
        Boolean obj3 = new Boolean("StUdYtONiGhT"); 
        Boolean obj4 = new Boolean("STUDYTONIGHT"); 
        Boolean obj5 = new Boolean("studytonight"); 

		System.out.println(obj1.hashCode()); 
        System.out.println(obj2.hashCode()); 
        System.out.println(obj3.hashCode()); 
        System.out.println(obj4.hashCode()); 
        System.out.println(obj5.hashCode()); 
    } 
}
	

boolean-class-example

8. equals(Object obj)

The equal() method is used to check if two boolean objects represent the same value. This method is used to compare two boolean values and returns true if both are equal, false otherwise.

It takes an argument that should not be null and returns a boolean value either true or false. Syntax of this method is given below.

Syntax:

	
boolean equals(Object obj)
	

Example:

Lets create an example to compare two boolean values whether they have the same value or not. It returns true if both the values are same.

	
public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        Boolean obj1 = new Boolean("True"); 
        Boolean obj2 = new Boolean(null);
        Boolean obj3 = new Boolean("StUdYtONiGhT"); 
        Boolean obj4 = new Boolean("STUDYTONIGHT"); 
        Boolean obj5 = new Boolean("studytonight"); 

		System.out.println(obj1.equals(obj2)); 
        System.out.println(obj2.equals(obj3)); 
        System.out.println(obj3.equals(obj4)); 
        System.out.println(obj4.equals(obj5)); 
        System.out.println(obj5.equals(obj1)); 
    } 
}
	

boolean-class-example-7.JPG

9. compareTo(Boolean b)

The compareTo() method is used to compare a Boolean instance with another. This method is specified in the Comparable interface that takes a single argument of Boolean type.

It returns 0 if both the object represents the same boolean value; a positive value if this object holds true while the argument represents false; and a negative value if this object represents false and the argument represents true value.

Syntax:

	
public int compareTo(Boolean b)
	

Example:

lets take an example to compare two boolean objects using compareTo(). This will return an int value based on the object comparison.

	
public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        Boolean obj1 = new Boolean("True"); 
        Boolean obj2 = new Boolean(null);
        Boolean obj3 = new Boolean("StUdYtONiGhT"); 
        Boolean obj4 = new Boolean("STUDYTONIGHT"); 
        Boolean obj5 = new Boolean("studytonight"); 

		System.out.println(obj1.compareTo(obj2)); 
        System.out.println(obj2.compareTo(obj3)); 
        System.out.println(obj3.compareTo(obj4)); 
        System.out.println(obj4.compareTo(obj5)); 
        System.out.println(obj5.compareTo(obj1)); 
    } 
}
	

boolean-class-example

10. int compare(boolean x, boolean y)

The compare() method is used to compare two boolean values. It takes two boolean arguments and return 0 if both values are equal; either negative or positive integer. The syntax of this method is given below.

Syntax:

	
public static int compare(boolean x, boolean y)
	

Example:

Lets take an example that compare two boolean values and returns integer based on the comparison.

	
public class booleanDemo1
{ 
    public static void main(String[] args) 
    { 
        boolean obj1 = true;
        boolean obj2 = false;
        boolean obj3 = true;
        boolean obj4 = false;
        boolean obj5 = true;

		System.out.println(Boolean.compare(obj1, obj2)); 
        System.out.println(Boolean.compare(obj2, obj3)); 
        System.out.println(Boolean.compare(obj3, obj4)); 
        System.out.println(Boolean.compare(obj4, obj5)); 
        System.out.println(Boolean.compare(obj5, obj1)); 
    } 
}
	

boolean-class-example