Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Java Program to Convert a String to Boolean

  • Last Updated : 09 Oct, 2021

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

Examples: 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections 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.

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));
    }
}
Output
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));
    }
}
Output
false
true
false



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!