Java Program to Convert a String to Boolean
Given string str, the task is to write a Java program to convert the given string to its boolean value.
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.
Strings – Strings in Java are objects that are supported internally by a char array. Since arrays are immutable, and strings are also a type of exceptional array that holds characters, therefore, strings are immutable as well.
Boolean – Boolean data type consists of only two values – true and false. If the string is true (ignoring case), the Boolean equivalent will be true, else false.
Approaches
There are numerous approaches to do the conversion of a given String to Boolean value in Java. A few of them are listed below.
- Using Boolean.parseBoolean() method
- Using Boolean.valueOf() method
1. Using Boolean.parseBoolean() method
- 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
// Java Program to Convert a String to Boolean 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
2. Using Boolean.valueOf() method
It is similar to the 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
// Java Program to Convert a String to Boolean 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


