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.


Boolean Class Constructor

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

public class BooleanMethods {
public static void main(String[] args) {

// Step 1: Create an object of Boolean class and assigning value to it
Boolean booleanobj=new Boolean(true);

// Step 2: Create a primitive type of boolean
boolean booleanPrimitive;

// Step 3: Assigning primitive value of booleanobj to booleanPrimitive value using booleanValue() method
booleanPrimitive = booleanobj.booleanValue();

String str = "Primitive value of booleanobj : " + booleanobj + " is " + booleanPrimitive;
System.out.println( str );
}
}

Output:

Primitive value of booleanobj : true is true

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

public class BooleanMethods {
public static void main(String[] args) {
// Step 1: create Boolean objects
Boolean boolean1, boolean2 ;

// Step 2: assign values to two objects created in step 1
boolean1 = new Boolean(true);
boolean2 = new Boolean(false);

int flag1 ; 

// Step 3: compare boolean1 with boolean2 using compareTo() method
flag1 = boolean1.compareTo(boolean2);

if( flag1 == 0 ){
System.out.println( " Both values are equal ");
}
else if( flag1 > 0 ){
System.out.println( " Object value is true " );
}
else if( flag1 < 0 ){
System.out.println( " Argument value is true " );
}
}
}

Output:

Object value is true

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

public class BooleanMethods {
public static void main(String[] args) {

// Step 1: create Boolean objects and assign values to them
Boolean boolean1, boolean2 ,boolean3;

boolean1 = new Boolean(true);
boolean2 = new Boolean(false);
boolean3 = new Boolean(false);
 
// Step 2: create a boolean primitive flag
boolean flag1, flag2; 

// Step 3: Assign the result of equals method to flag
flag1 = boolean1.equals(boolean2);
flag2 = boolean3.equals(boolean2); 

System.out.println( "Are boolean1 and boolean2 equal? : " +flag1  );
System.out.println( "Are boolean2 and boolean3 equal? : " +flag2  );
}
}

Output:

Are boolean1 and boolean2 equal? : false
Are boolean2 and boolean3 equal? : true

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

public class BooleanMethods {
public static void main(String[] args) { 

// Step 1: create boolean primitives.
boolean boolean1, boolean2; 

// Step 2: Set values for system properties flag1 and flag2
System.setProperty("flag1","true");
System.setProperty("flag2","xyz");

// Step 3: Assign system properties to boolean primitive types using getBoolean() method
boolean1 = Boolean.getBoolean("flag1");
boolean2 = Boolean.getBoolean("flag2");

System.out.println( "Boolean value of System property flag1 is : " +boolean1  );
System.out.println( "Boolean value of System property flag2 is : " +boolean2   );
}
}

Output:

Boolean value of System property flag1 is : true
Boolean value of System property flag2 is : false 

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

public class BooleanMethods {
public static void main(String[] args) { 

// Step 1: create 2 Boolean objects.
Boolean boolean1, boolean2;
boolean1= new Boolean(true);
boolean2= new Boolean(false); 

// Step 2: Hash Code values are obtained using hashCode() method. 
System.out.println( " Hashcode value of boolean1 is : " + boolean1.hashCode() );
System.out.println( " Hashcode value of boolean2 is : " + boolean2.hashCode() );
}
}

Output:

Hashcode value of boolean1 is : 1231
Hashcode value of boolean2 is : 1237

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

public class BooleanMethods {
public static void main(String[] args) { 

// Step 1: Create two strings and assign values to them
String string1 = "tRuE";
String string2 = "xyz"; 

// Step 2: Create 2 boolean primitives
boolean boolean1, boolean2; 

//Step 3: Assigning values to primitive types using parseBoolean() method
boolean1 = Boolean.parseBoolean(string1);
boolean2 = Boolean.parseBoolean(string2);

System.out.println( " Value of boolean1 is : " + boolean1 );
System.out.println( " Value of boolean1 is : " + boolean2 );
}
}

Output:

Value of boolean1 is : true
Value of boolean1 is : false

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

public class BooleanMethods {
public static void main(String[] args) {

// Step 1: create Boolean objects.
Boolean boolean1, boolean2, boolean3;

// Step 2: Assign values to objects created in Step 1
boolean1= new Boolean(true);
boolean2= new Boolean(false);
boolean3= new Boolean(null); 

// Step 3: Assign String values of Boolean objects using toString() method
String string1= boolean1.toString();
String string2= boolean2.toString();
String string3= boolean3.toString(); 

System.out.println( " String value of Boolean object boolean1 is  " + string1 );
System.out.println( " String value of Boolean object boolean2 is  " + string2 );
System.out.println( " String value of Boolean object boolean3 is  " + string3 );
}
}

Output:

String value of Boolean object boolean1 is  true
String value of Boolean object boolean2 is  false
String value of Boolean object boolean3 is  false

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

public class BooleanMethods {
public static void main(String[] args) { 

// Step 1: create boolean primitives.
boolean boolean1, boolean2; 

// Step 2: Assign values to boolean primitives created in Step 1
boolean1= true;
boolean2= false; 

// Step 3: Assign String values of boolean primitives using toString() method
String string1= Boolean.toString(boolean1);
String string2= Boolean.toString(boolean2);
 
System.out.println( " String value of boolean primitive boolean1 is  " +string1 );

System.out.println( " String value of boolean primitive boolean2 is  " +string2 );
}
}

Output:

String value of boolean primitive boolean1 is  true
String value of boolean primitive boolean2 is  false

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

public class BooleanMethods {
public static void main(String[] args) { 

// Step 1: create Boolean objects
Boolean boolean1, boolean2; 

// Step 2: create boolean primitives and assign values to them
boolean boolPrimitive1 = true;
boolean boolPrimitive2 = false; 

// Step 3: Assigning values of primitive types to Boolean objects
boolean1 = Boolean.valueOf(boolPrimitive1);
boolean2 = Boolean.valueOf(boolPrimitive2); 

System.out.println( " Boolean instance of primitive type boolPrimitive1 is : " + boolean1 );
System.out.println( " Boolean instance of primitive type boolPrimitive2 is : " + boolean2  );
}
}

Output:

Boolean instance of primitive type boolPrimitive1 is : true
Boolean instance of primitive type boolPrimitive2 is : false

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

public class BooleanMethods {
public static void main(String[] args) { 

// Step 1: Create Boolean objects
Boolean boolean1, boolean2, boolean3, boolean4; 

// Step 2: Create  String's and assign values to them
String string1 = "abc";
String string2 = "false";
String string3= null;
String string4= "TrUe";

//Step 3: Assigning boolean value of Strings to Boolean objects
boolean1 = Boolean.valueOf(string1);
boolean2 = Boolean.valueOf(string2);
boolean3 = Boolean.valueOf(string3);
boolean4 = Boolean.valueOf(string4); 

System.out.println( " Boolean value of String1 is : "  + boolean1 );
System.out.println( " Boolean value of String2 is : "  + boolean2 );
System.out.println( " Boolean value of String3 is : "  + boolean3 );
System.out.println( " Boolean value of String4 is : "  + boolean4 );
}
}

Output:

Boolean value of String1 is : false
Boolean value of String2 is : false
Boolean value of String3 is : false
Boolean value of String4 is : true

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