Java Program to Convert String to Boolean

16 Apr, 2022
To convert String to boolean in Java, you can use Boolean.parseBoolean(string). But if you want to convert String to Boolean object then use the method Boolean.valueOf(string) method.
Boolean data type consists of only two values i.e true and false. If the string is true (ignoring case), the Boolean equivalent will be true, else false.
Tip: In Java, only true and false are returned as boolean not 0 and 1.
Example: 
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.
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) 
Example :
Java
// Java Program to Convert a String to Boolean// Using parseBoolean() Method of Boolean Class // Main classclass GFG {     // Method 1    // To convert a string to its boolean value    public static boolean stringToBoolean(String str)    {         // Converting a given string to its primitive        // boolean value using parseBoolean() method        boolean b1 = Boolean.parseBoolean(str);         // Return primitive boolean value        return b1;    }     // Method 2    // Main driver method    public static void main(String args[])    {         // Given String str        String str = "yes";         // Printing the desired boolean value        System.out.println(stringToBoolean(str));         // Given String str        str = "true";         // Printing the desired boolean value        System.out.println(stringToBoolean(str));         // Given String str        str = "false";         // Printing the desired boolean value        System.out.println(stringToBoolean(str));    }}
false
true
false
It is similar to the above method as discussed just a little difference lies as it returns a boolean object instead of a primitive boolean value.
Syntax: 
boolean boolValue = Boolean.valueOf(String str) 
Example:
Java
// Java Program to Convert a String to Boolean Object// Using valueOf() Method of Boolean Class // Main classclass GFG {     // Method 1    // To convert a string to its boolean object    public static boolean stringToBoolean(String str)    {         // Converting a given string        // to its boolean object        // using valueOf() method        boolean b1 = Boolean.valueOf(str);         // Returning boolean object        return b1;    }     // Method 2    // Main driver method    public static void main(String args[])    {        // Given input string 1        String str = "yes";         // Printing the desired boolean        System.out.println(stringToBoolean(str));         // Given input string 2        str = "true";         // Printing the desired boolean        System.out.println(stringToBoolean(str));         // Given input string 3        str = "false";         // Printing the desired boolean        System.out.println(stringToBoolean(str));    }}
false
true
false
nishkarshgandhi
solankimayank
Java-Boolean
Java-String-Programs
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.