Java – how to check if a String is a boolean!

I stumbled upon this “problem” regularly, for example I have a String value and want to check if this can be parsed to a boolean.

Boolean.getBoolean(String name) – javadocs

Returns true if and only if the system property named by the argument exists and is equal to the string “true”.

System.out.println("Boolean.getBoolean(true): " + Boolean.getBoolean("true"));
System.out.println("Boolean.getBoolean(false): " + Boolean.getBoolean("false"));
System.out.println("Boolean.getBoolean(gibberish): " + Boolean.getBoolean("gibberish"));
System.out.println("Boolean.getBoolean(yes): " + Boolean.getBoolean("yes"));

// Output
Boolean.getBoolean(true): false
Boolean.getBoolean(false): false
Boolean.getBoolean(gibberish): false
Boolean.getBoolean(yes): false

here you can see that everything is false (probably because of the system property)

Boolean.parseBoolean(String s) – javadocs

Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string “true”.

System.out.println("Boolean.parseBoolean(true): " + Boolean.parseBoolean("true"));
System.out.println("Boolean.parseBoolean(false): " + Boolean.parseBoolean("false"));
System.out.println("Boolean.parseBoolean(gibberish): " + Boolean.parseBoolean("gibberish"));
System.out.println("Boolean.parseBoolean(yes): " + Boolean.parseBoolean("yes"));

// Output
Boolean.parseBoolean(true): true
Boolean.parseBoolean(false): false
Boolean.parseBoolean(gibberish): false
Boolean.parseBoolean(yes): false

this is interesting, here you can see that we are parsing the string and always get false except when the String is “true”. In this Case we cant distinguish now if the String is a boolean or not, because we get only true or false even if the String is neither true nor false.

Boolean.valueOf(String s) – javadocs

Returns a Boolean with a value represented by the specified string.

System.out.println("Boolean.valueOf(true): " + Boolean.valueOf("true")); 
System.out.println("Boolean.valueOf(false): " + Boolean.valueOf("false")); 
System.out.println("Boolean.valueOf(gibberish): " + Boolean.valueOf("gibberish")); 
System.out.println("Boolean.valueOf(yes): " + Boolean.valueOf("yes")); 

// Output 
Boolean.valueOf(true): true 
Boolean.valueOf(false): false 
Boolean.valueOf(gibberish): false 
Boolean.valueOf(yes): false

Same result with valueOf, we still get false even though it is not a boolean.

Easy solution

Personally I like to have a method to check if the String is a boolean so that I have only two lines of code for it. If I only use the if statement to parse the String to a boolean I can’t be sure if it is really a boolean (especially important if you need to handle some user input in some configuration files) like we could see above. The Solution is pretty easy but I thought there is some easier way. All you have to do is to check if the String equals to true or false.

// Method
public boolean isBoolean(Object o) { 
    return o.toString().equalsIgnoreCase("true") || o.toString().equalsIgnoreCase("false"); 

// Calling code
System.out.println("this.isBoolean(true): " + this.isBoolean("true")); 
System.out.println("this.isBoolean(false): " + this.isBoolean("false")); 
System.out.println("this.isBoolean(gibberish): " + this.isBoolean("gibberish")); 
System.out.println("this.isBoolean(yes): " + this.isBoolean("yes")); 

// Output 
this.isBoolean(true): true 
this.isBoolean(false): true 
this.isBoolean(gibberish): false 
this.isBoolean(yes): false

And there you have it, even if you have lower or uppercase versions of true and false you will always know if it is a boolean or not.

System.out.println("this.isBoolean(TrUe): " + this.isBoolean("TrUe")); 
System.out.println("this.isBoolean(FaLsE): " + this.isBoolean("FaLsE")); 
System.out.println("this.isBoolean(TRUE): " + this.isBoolean("TRUE")); 
System.out.println("this.isBoolean(FALSE): " + this.isBoolean("FALSE")); 

// Output 
this.isBoolean(TrUe): true 
this.isBoolean(FaLsE): true 
this.isBoolean(TRUE): true 
this.isBoolean(FALSE): true