How to convert String object to Boolean Object?
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
orBoolean.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");
Suresh
Updated on July 10, 2022Comments
-
Suresh almost 2 years
How to convert
String
object toBoolean
object? -
Alex Feinman over 14 yearswouldn't assigning Boolean.valueOf to boolaen2 be auto-unboxed anyway? I'm not seeing the difference to parseBoolean here.
-
Brandon Belvin over 13 yearsThe 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 over 13 yearsThis 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 about 11 yearswhat if i use
boolean boolean2 = Boolean.valueOf("true");
-
paulo.albuquerque almost 10 yearsAlthough 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 almost 9 yearsif 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 about 8 yearsNo 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 over 6 yearsWhat is BooleanUtils?
-
Dhana over 6 yearsorg.apache.commons.lang3.BooleanUtils is from Apache commons Lang API.
-
saw303 almost 6 yearsThis is a very hacky proposal of converting a String into a Boolean and just makes no sense.
-
Admin almost 5 yearsHeh... 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 over 4 yearsplus for the efforts gathering all use cases.
-
ddekany about 3 yearsBeware,
BooleanUtils
is still wrong (although less so thanBoolean
methods), as it will not throw exception if the value neither recognized astrue
, orfalse
. Soture
(a typo that someone does somewhere) is still blindly assumed to meanfalse
. I can't find anything in Guava either (like there's noBooleans.stringConverter
as of 23.0). -
ddekany about 3 yearsBecause 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 almost 2 yearsWhat about -1 and 0 ;o)