Take the 2-minute tour ×

I did a little search on this but couldn't find anything useful.

The point being that if String value is either "true" or "false" the return value should be true. In every other value it should be false.

I tried these:

String value = "false";
System.out.println("test1: " + Boolean.parseBoolean(value));
System.out.println("test2: " + Boolean.valueOf(value));
System.out.println("test3: " + Boolean.getBoolean(value));

All functions returned false :(

share|improve this question
Even if you set value = "true"? –  moxn Nov 19 '09 at 12:16
Anyway, if you try one of the proposed approaches you should use String.equalsIgnoreCase(String s) to be on the safe side. –  moxn Nov 19 '09 at 12:20
Did you try this: tinyurl.com/ycovuqg –  Alexander Pogrebnyak Nov 19 '09 at 12:35
It appears that you are using these methods incorrectly. Please take a look at the Java docs for Boolean. j2ee.me/javase/6/docs/api/java/lang/Boolean.html –  Dan Polites Nov 19 '09 at 12:40
Ragnar, the reason you're not finding it is that everyone has a different definition of what a boolean should be in string form. Is "TRUE" valid"? What about "True"? "TRUE" in double-byte characters? For perl hackers, having "1" not be true is offensive. :) So your method is going to have to be explicit about what it desires. –  Alex Feinman Nov 19 '09 at 13:57

12 Answers 12

up vote 35 down vote accepted
  • parseBoolean(String) returns true if the String is (case-insensitive) "true", otherwise false
  • valueOf(String) ditto, returns the canonical Boolean Objects
  • getBoolean(String) is a red herring; it fetches the System property of the given name and compares that to "true"

There exists no method to test whether a String encodes a Boolean; for all practical effects, any non-"true"-String is "false".

share|improve this answer
OK, this what I needed to know. I wonder why it is practical to have have any non-"true"-String as false? I mean in my mind valid inputs should be true and false and everything else is just not boolean input. –  Ragnar Nov 19 '09 at 13:01
True, one could imagine a "BooleanFormatException" Exception, in analogy to the NumberFormatException that occurs when you try to parse a non-number String. I suppose it was deemed unnecessary at the time the library was written (1996!), for the targeted environments (embedded systems like set-top boxes and browsers). –  mfx Nov 19 '09 at 13:33
Well there is no BooleanFormatException, but by using Apache Commons' BooleanUtils.toBooleanObject, if a string cannot be parsed, a null value will be returned, which can be quite useful. –  Peymankh Oct 22 '12 at 11:56
return "true".equals(value) || "false".equals(value);
share|improve this answer
Instead of hard-coding "true" and "false", you can use true.toString and false.toString, just in case the string representations of "true" and "false" ever change ;) –  Ben James Nov 19 '09 at 12:19
In other languages, "1" and "0" are also considered valid for boolean string parsing, so I don't quite like the "hardcode true and false"-approach. OTOH the OP introduced the restriction to true and false, so well... –  Thorsten Dittmar Nov 19 '09 at 12:26
I was hoping to avoid this, since the input value could also be " true" or "True" or "TRUE" etc. –  Ragnar Nov 19 '09 at 12:34
@Ragnar As I pointed out in a comment above, you should use String.equalsIgnoreCase(String s). –  moxn Nov 19 '09 at 12:50
@moxn Thanks. I'll probably just wrap this into one funtion to check both true and false so I get bit cleaner code. –  Ragnar Nov 19 '09 at 13:10

The methods you're calling on the Boolean class don't check whether the string contains a valid boolean value, but they return the boolean value that represents the contents of the string: put "true" in string, they return true, put "false" in string, they return false.

You can surely use these methods, however, to check for valid boolean values, as I'd expect them to throw an exception if the string contains "hello" or something not boolean.

Wrap that in a Method ContainsBoolString and you're go.

By the way, in C# there are methods like bool Int32.TryParse(string x, out int i) that perform the check whether the content can be parsed and then return the parsed result.

int i;
if (Int32.TryParse("Hello", out i))
  // Hello is an int and its value is in i
  // Hello is not an int

Benchmarks indicate they are way faster than the following:

int i;
   i = Int32.Parse("Hello");
   // Hello is an int and its value is in i
   // Hello is not an int

Maybe there are similar methods in Java? It's been a while since I've used Java...

share|improve this answer
String value = "True";
boolean result = value.equalsIgnoreCase("true") ? true : false;
share|improve this answer
if ("true".equals(value) || "false".equals(value)) {
  // do something
} else {
  // do something else
share|improve this answer

Yes, but, didn't you parse "false"? If you parse "true", then they return true.

Maybe there's a misunderstanding: the methods don't test, if the String content represents a boolean value, they evaluate the String content to boolean.

share|improve this answer
I kind of figured it out too. –  Ragnar Nov 19 '09 at 12:40

Can also do it by regex:

Pattern queryLangPattern = Pattern.compile("true|false", Pattern.CASE_INSENSITIVE);
Matcher matcher = queryLangPattern.matcher(booleanParam);
return matcher.matches();
share|improve this answer

Actually, checking for a Boolean type in a String (which is a type) is impossible. Basically you're asking how to do a 'string compare'.

Like others stated. You need to define when you want to return "true" or "false" (under what conditions). Do you want it to be case(in)sensitive? What if the value is null?

I think Boolean.valueOf() is your friend, javadoc says:

Returns a Boolean with a value represented by the specified String. The Boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".

Example: Boolean.valueOf("True") returns true.
Example: Boolean.valueOf("yes") returns false.

share|improve this answer
return value.equals("false") || value.equals("true");
share|improve this answer
Beware of value == null –  lutz Nov 19 '09 at 12:18
You could wrap the return statement into an try/catch construct, catch NPE and handle this exceptional state to return the real ... just kidding ;-) –  Andreas_D Nov 19 '09 at 12:26
@lutz - bad advice. A null is almost certainly a bug and should result in an NPE. Testing for null to avoid the NPE is almost certainly going to obscure the bug. You should only test for null if it is explicitly stated that null has meaning. –  Stephen C Nov 19 '09 at 12:46

Something you should also take into consideration is character casing...

Instead of:


Do this:

return value.equalsIgnoreCase("false") || value.equalsIgnoreCase("true");
share|improve this answer

I suggest that you take a look at the Java docs for these methods. It appears that you are using them incorrectly. These methods will not tell you if the string is a valid boolean value, but instead they return a boolean, set to true or false, based on the string that you pass in, "true" or "false".


share|improve this answer

Well for this, also have a look at org.apache.commons.lang.BooleanUtils#toBoolean(java.lang.String), along with many other useful functions.

share|improve this answer

Your Answer


