How to convert String object to Boolean Object?

607,603

Solution 1

Try (depending on what result type you want):

Boolean boolean1 = Boolean.valueOf("true");
boolean boolean2 = Boolean.parseBoolean("true");

Advantage:

  • Boolean: this does not create new instances of Boolean, so performance is better (and less garbage-collection). It reuses the two instances of either Boolean.TRUE or Boolean.FALSE.
  • boolean: no instance is needed, you use the primitive type.

The official documentation is in the Javadoc.


UPDATED:

Autoboxing could also be used, but it has a performance cost.
I suggest to use it only when you would have to cast yourself, not when the cast is avoidable.

Solution 2

You have to be carefull when using Boolean.valueOf(string) or Boolean.parseBoolean(string). The reason for this is that the methods will always return false if the String is not equal to "true" (the case is ignored).

For example:

Boolean.valueOf("YES") -> false

Because of that behaviour I would recommend to add some mechanism to ensure that the string which should be translated to a Boolean follows a specified format.

For instance:

if (string.equalsIgnoreCase("true") || string.equalsIgnoreCase("false")) {
    Boolean.valueOf(string)
    // do something   
} else {
    // throw some exception
}

Solution 3

Boolean b = Boolean.valueOf(string);

The value of b is true if the string is not a null and equal to true (ignoring case).

Solution 4

Beside the excellent answer of KLE, we can also make something more flexible:

boolean b = string.equalsIgnoreCase("true") || string.equalsIgnoreCase("t") || 
        string.equalsIgnoreCase("yes") || string.equalsIgnoreCase("y") || 
        string.equalsIgnoreCase("sure") || string.equalsIgnoreCase("aye") || 
        string.equalsIgnoreCase("oui") || string.equalsIgnoreCase("vrai");

(inspired by zlajo's answer... :-))

Solution 5

boolean b = string.equalsIgnoreCase("true");
Share:
607,603
Suresh
Author by

Suresh

Updated on July 10, 2022

Comments

  • Suresh
    Suresh almost 2 years

    How to convert String object to Boolean object?

  • Alex Feinman
    Alex Feinman over 14 years
    wouldn't assigning Boolean.valueOf to boolaen2 be auto-unboxed anyway? I'm not seeing the difference to parseBoolean here.
  • Brandon Belvin
    Brandon Belvin over 13 years
    The biggest problem is that Boolean will not exception out when it sees something it shouldn't accept. It will return true for anything it sees as "true" and will return false for everything else. If you're trying to enforce matching a string to an appropriate boolean value, you'll have to add extra logic to catch illegal cases manually.
  • Brandon Belvin
    Brandon Belvin over 13 years
    This is the best example I've seen and what should have been implemented in the Boolean type to begin with. Throwing an exception for invalid Boolean value is important for many applications.
  • Vipin Verma
    Vipin Verma about 11 years
    what if i use boolean boolean2 = Boolean.valueOf("true");
  • paulo.albuquerque
    paulo.albuquerque almost 10 years
    Although the question's text isn't explicit, this is a question about Java. At least it's tagged that way. This answer can confuse people.
  • amit kate
    amit kate almost 9 years
    if String object is null then Boolean.valueOf(String) will return false .But Boolean can hold null value also .can you provide any help for this.
  • electricalbah
    electricalbah about 8 years
    No thats not totally true. here is the underlying implementation of parseBoolean public static boolean parseBoolean(String s) { return ((s != null) && s.equalsIgnoreCase("true")); }
  • james.garriss
    james.garriss over 6 years
    What is BooleanUtils?
  • Dhana
    Dhana over 6 years
    org.apache.commons.lang3.BooleanUtils is from Apache commons Lang API.
  • saw303
    saw303 almost 6 years
    This is a very hacky proposal of converting a String into a Boolean and just makes no sense.
  • Admin
    Admin almost 5 years
    Heh... If you need to write such code, then you don't need to call Boolean.valueOf. Instead you can simple restructure this if statement so it will do what you want ;-)
  • Gaurav
    Gaurav over 4 years
    plus for the efforts gathering all use cases.
  • ddekany
    ddekany about 3 years
    Beware, BooleanUtils is still wrong (although less so than Boolean methods), as it will not throw exception if the value neither recognized as true, or false. So ture (a typo that someone does somewhere) is still blindly assumed to mean false. I can't find anything in Guava either (like there's no Booleans.stringConverter as of 23.0).
  • ddekany
    ddekany about 3 years
    Because this is the top answer, could you update it to warn people not to use these dangerous methods? (I'm not aware of any good implementation in common libraries unfortunately.)
  • Minty
    Minty almost 2 years
    What about -1 and 0 ;o)