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)); } } |
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)); } } |
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.
Recommended Posts:
- Java Program to convert boolean to integer
- Java Program to convert integer to boolean
- Convert a List of String to a comma separated String in Java
- Convert a Set of String to a comma separated String in Java
- Convert an ArrayList of String to a String array in Java
- Convert Set of String to Array of String in Java
- How to convert Date to String in Java
- Convert String to Date in Java
- How to convert an Array to String in Java?
- Convert String to Double in Java
- Convert List of Characters to String in Java
- Convert Character Array To String In Java
- Program to convert IntStream to String in Java
- Convert a String to Character array in Java
- Program to convert String to IntStream in Java
- Convert a String to a List of Characters in Java
- Program to convert set of String to set of Integer in Java
- How to Convert a Byte value to String value in Java with Examples
- How to Convert a String value to Byte value in Java with Examples
- How to Convert a Short value to String value in Java with Examples
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.

