How to convert a string to boolean in Java

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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));
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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));
    }
}

chevron_right


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.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.