Convert String To Boolean in Java

In Java, converting a String to a Boolean is a common operation when dealing with user inputs or configuration files. A Boolean value represents either true or false. In this blog post, we will explore different methods to convert a String to a Boolean and provide practical examples with outputs to illustrate each approach. 

Method 1: Using Boolean.parseBoolean() Method 

The Boolean.parseBoolean() method parses the string argument as a boolean. It returns the boolean value true if the lowercase input string is equal to "true". Otherwise, it returns false. 

Example:

public class StringToBooleanExample {
    public static void main(String[] args) {
        String trueString = "true";
        String falseString = "false";
        String invalidString = "invalid";

        // Convert String to Boolean using parseBoolean()
        boolean booleanValue1 = Boolean.parseBoolean(trueString);
        boolean booleanValue2 = Boolean.parseBoolean(falseString);
        boolean booleanValue3 = Boolean.parseBoolean(invalidString);

        System.out.println("Input String: " + trueString);
        System.out.println("Boolean Value: " + booleanValue1);

        System.out.println("Input String: " + falseString);
        System.out.println("Boolean Value: " + booleanValue2);

        System.out.println("Input String: " + invalidString);
        System.out.println("Boolean Value: " + booleanValue3);
    }
}

Output:

Input String: true
Boolean Value: true
Input String: false
Boolean Value: false
Input String: invalid
Boolean Value: false

Method 2: Using Boolean.valueOf() Method 

The Boolean.valueOf() method converts a String to a Boolean object. It returns the Boolean.TRUE object if the input string is equal to "true" (case-insensitive). Otherwise, it returns the Boolean.FALSE object.

Example:
public class StringToBooleanExample {
    public static void main(String[] args) {
        String trueString = "True";
        String falseString = "False";
        String invalidString = "Invalid";

        // Convert String to Boolean using valueOf()
        Boolean booleanValue1 = Boolean.valueOf(trueString);
        Boolean booleanValue2 = Boolean.valueOf(falseString);
        Boolean booleanValue3 = Boolean.valueOf(invalidString);

        System.out.println("Input String: " + trueString);
        System.out.println("Boolean Value: " + booleanValue1);

        System.out.println("Input String: " + falseString);
        System.out.println("Boolean Value: " + booleanValue2);

        System.out.println("Input String: " + invalidString);
        System.out.println("Boolean Value: " + booleanValue3);
    }
}

Output:

Input String: True
Boolean Value: true
Input String: False
Boolean Value: false
Input String: Invalid
Boolean Value: false

Method 3: Using Boolean Constructor 

The Boolean class has a constructor that takes a String argument and creates a Boolean object. It returns Boolean.TRUE if the argument is not null and is equal to "true" (case-insensitive). Otherwise, it returns Boolean.FALSE.

Example:

public class StringToBooleanExample {
    public static void main(String[] args) {
        String trueString = "YES";
        String falseString = "NO";
        String invalidString = "Invalid";

        // Convert String to Boolean using constructor
        Boolean booleanValue1 = new Boolean(trueString);
        Boolean booleanValue2 = new Boolean(falseString);
        Boolean booleanValue3 = new Boolean(invalidString);

        System.out.println("Input String: " + trueString);
        System.out.println("Boolean Value: " + booleanValue1);

        System.out.println("Input String: " + falseString);
        System.out.println("Boolean Value: " + booleanValue2);

        System.out.println("Input String: " + invalidString);
        System.out.println("Boolean Value: " + booleanValue3);
    }
}

Output:

Input String: YES
Boolean Value: true
Input String: NO
Boolean Value: false
Input String: Invalid
Boolean Value: false

Conclusion: 

Converting a String to a Boolean in Java is a straightforward process. You can use methods like Boolean.parseBoolean(), Boolean.valueOf(), or the Boolean constructor to achieve this. In this blog post, we provided practical examples with outputs to demonstrate each method's effectiveness. Always be cautious with the input String, as unexpected values can lead to incorrect Boolean conversion results.

Comments