Skip to content
Related Articles

Related Articles

Improve Article

How to convert a string to boolean in Java

  • Last Updated : 19 Jul, 2020

Given a string str, the task is to convert the given string to its boolean value.

Boolean datatype consists of only two values: true and false. If the string is true (ignoring case), the Boolean equivalent will be true, else false.

Examples: 

Input: str = “true”

Output: true



Explanation: The boolean equivalent of true is true itself.

Input: str = “false” 

Output: false 

Explanation: The boolean equivalent of false is false itself.

Input: str = “yes” 

Output: false

Explanation: The boolean equivalent of yes is false since the given value is not equal to true.

 

The methods to convert the string to its boolean value are as follows:



Method 1: 

  • Using Boolean.parseBoolean() method. This is the most common method to convert String to boolean.
  • This method is used to convert a given string to its primitive boolean value.
  • If the given string contains the value true ( ignoring cases) then this method returns true, if the string contains any other value other than true then the method returns false.

Syntax:

boolean boolValue = Boolean.parseBoolean(String str)

Below is the implementation of the above approach:

Java




class GFG {
  
    // Function to convert a string
    // to its boolean value
    public static boolean
    stringToBoolean(String str)
    {
        // convert a given string to
        // its primitive boolean value
        // using parseBoolean() method
        boolean b1
            = Boolean.parseBoolean(str);
  
        // returns primitive boolean value
        return b1;
    }
  
    // Driver code
    public static void main(String args[])
    {
        // Given String str
        String str = "yes";
  
        // print the result
        System.out.println(
            stringToBoolean(str));
  
        // Given String str
        str = "true";
  
        // print the result
        System.out.println(
            stringToBoolean(str));
  
        // Given String str
        str = "false";
  
        // print the result
        System.out.println(
            stringToBoolean(str));
    }
}
Output
false
true
false

Method 2:  

  • Using Boolean.valueOf() method.
  • It is similar to Boolean.parseBoolean() method, but it returns a boolean object instead of a primitive boolean value.

Syntax:

boolean boolValue = Boolean.valueOf(String str)

Below is the implementation of the above approach:

Java




class GFG {
  
    // Function to convert a string
    // to its boolean object
    public static boolean
    stringToBoolean(String str)
    {
        // convert a given string to
        // its boolean object using
        // valueOf() method
        boolean b1 = Boolean.valueOf(str);
  
        // returns boolean object
        return b1;
    }
  
    // Driver code
    public static void main(String args[])
    {
        // Given String str
        String str = "yes";
  
        // print the result
        System.out.println(
            stringToBoolean(str));
  
        // Given String str
        str = "true";
  
        // print the result
        System.out.println(
            stringToBoolean(str));
  
        // Given String str
        str = "false";
  
        // print the result
        System.out.println(
            stringToBoolean(str));
    }
}
Output
false
true
false

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :