How to Convert String to Boolean in Java

In this tutorial, we will learn how to convert string to primitive type boolean or Boolean wrapper class object in Java easily.

There are three ways to convert a string to a boolean data type. They are as follows:

  • Convert using Boolean.parseBoolean()
  • Convert using Boolean.valueOf()
  • Converting using new Boolean(String).booleanValue()

How to convert string to boolean in Java

Convert String to boolean using Boolean.parseBoolean()


To convert string to a boolean, we use Boolean.parseBoolean() method from the Boolean wrapper class.

The Boolean class wraps a boolean value of a primitive type in an object. An object of type Boolean contains a single variable whose type is boolean.

The parseDouble() of Boolean wrapper class is the static utility method. So, we do not need to create an object of class to call it. We can call it simply using its class name.

The general signature of parseBoolean() method is as below:

public static boolean parseBoolean(String s)

This method accepts a string containing the boolean representation to be parsed. It returns the boolean value true if string contains “true” or “TRUE”. Here, the case is ignored. It returns false boolean value if the string contains any other value except “true”.

Let’s create a simple program to convert a string to a boolean value.

Program code 1:

// Java Program to demonstrate the conversion of string into boolean using Boolean.parseBoolean().
package stringPrograms;
public class StringToBoolean {
public static void main(String[] args) 
{		
 String str1 = "true";  
 String str2 = "TRUE";  
 String str3 = "Yes";  

// Convert string into boolean using parseBoolean() method. 
  boolean b1 = Boolean.parseBoolean(str1);  
  boolean b2 = Boolean.parseBoolean(str2);  
  boolean b3 = Boolean.parseBoolean(str3);  
 
  System.out.println(b1);  
  System.out.println(b2);  
  System.out.println(b3);  
 }
}
Output:
      true
      true
      false

String to Boolean Conversion using Boolean.valueOf()


To convert a string to a Boolean object, we normally use Boolean.valueOf() method of Java Boolean wrapper class. The general signature of this method is as below:

public static Boolean valueOf​(String str)

This method converts a string str containing a boolean value into a Boolean class object and returns that object holding the value of the specified string.

Let’s take a very simple example program to convert String to Boolean object in Java.

Program code 2:

// Java Program to demonstrate the conversion of string into Boolean object using Boolean.parseBoolean().
package stringPrograms;
public class StringToBoolean {
public static void main(String[] args) 
{		
 String str1 = "true";  
 String str2 = "TRUE";  
 String str3 = "Yes";  

// Convert string into Boolean class object using valueOf() method. 
  Boolean b1 = Boolean.valueOf(str1);  
  Boolean b2 = Boolean.valueOf(str2);  
  Boolean b3 = Boolean.valueOf(str3);  
 
  System.out.println(b1);  
  System.out.println(b2);  
  System.out.println(b3);  
 }
}
Output:
      true
      true
      false

Converting String into boolean using new Boolean(String).booleanValue()


Another alternative approach is to create an instance of Boolean class and then call booleanValue() method of Boolean wrapper class.

The booleanValue() method converts the boolean object into primitive boolean type value. This is called “unboxing” in Java.

Let’s create a Java program to convert String into a boolean value using booleanValue() method.

Program code 3:

// Java Program to demonstrate the conversion of string into boolean using Boolean(String).booleanValue()().
package stringPrograms;
public class CharToString {
public static void main(String[] args) 
{		
 String str1 = "true";  
 String str2 = "TRue";  
 String str3 = "Yes";  

// Convert string into boolean value using Boolean(String).booleanValue() method.  
   boolean b1 = new Boolean(str1).booleanValue();
   boolean b2 = new Boolean(str2).booleanValue();
   boolean b3 = new Boolean(str3).booleanValue();
   
   System.out.println(b1);  
   System.out.println(b2);  
   System.out.println(b3);  
 }
}
Output:
      true
      true
      false

In this tutorial, you learned how to convert a string to primitive boolean type value in Java in different ways. Hope that you will have understood all the methods for converting string to boolean and practiced all programs.
Thanks for reading!!!
Next ⇒ Convert Boolean to String in Java⇐ Prev Next ⇒