To convert String to boolean in Java, you can use one of the following methods:
- Boolean.parseBoolean() method
- BooleanUtils.toBoolean() method
- Boolean.valueOf() method if we need a Boolean object instead of a primive
Convert String to boolean in Java using the Boolean.parseBoolean() method
With the parseBoolean(String s) method of the Boolean class, if we want to get a boolean true, the provided String must be “true” ignoring cases (“True“, “TRUE“, “truE” etc…). Otherwise, we will get false.
Example
class Test { public static void main(String[] args) { System.out.println("String 'true' to boolean: " + Boolean.parseBoolean("true")); System.out.println("String 'TRUE' to boolean: " + Boolean.parseBoolean("TRUE")); System.out.println("String 'True' to boolean: " + Boolean.parseBoolean("True")); System.out.println("String 'false' to boolean: " + Boolean.parseBoolean("false")); System.out.println("String 'False' to boolean: " + Boolean.parseBoolean("False")); System.out.println("String 'abc' to boolean: " + Boolean.parseBoolean("abc")); System.out.println("String 'TruE' to boolean: " + Boolean.parseBoolean("TruE")); } }
Parse String to boolean using the BooleanUtils.toBoolean() method
There is one helpful method toBoolean(String s) from the BooleanUtils class that belongs to the Apache Commons library. The provided String must be “true” value ignoring cases in order to get a boolean true, otherwise false.
If you are using a Maven project, add this dependency to your pom.xml:
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.12.0</version> </dependency>
If you are not yet familiar with Maven, be sure to check out this tutorial Create Java Project with Maven.
Example
import org.apache.commons.lang3.BooleanUtils; class Test { public static void main(String[] args) { System.out.println("String 'true' to boolean: " + BooleanUtils.toBoolean("true")); System.out.println("String 'TRUE' to boolean: " + BooleanUtils.toBoolean("TRUE")); System.out.println("String 'True' to boolean: " + BooleanUtils.toBoolean("True")); System.out.println("String 'false' to boolean: " + BooleanUtils.toBoolean("false")); System.out.println("String 'False' to boolean: " + BooleanUtils.toBoolean("False")); System.out.println("String 'abc' to boolean: " + BooleanUtils.toBoolean("abc")); System.out.println("String 'TruE' to boolean: " + BooleanUtils.toBoolean("TruE")); } }
Parse String to boolean in Java using the Boolean.valueOf() method
If we need a Boolean object instead of a primitive type, we can use the overloaded version of a valueOf() method, which accepts a String and returns a Boolean object.
Example
class Test { public static void main(String[] args) { Boolean b1 = Boolean.valueOf("true"); Boolean b2 = Boolean.valueOf("True"); Boolean b3 = Boolean.valueOf("false"); Boolean b4 = Boolean.valueOf("abc"); System.out.println(b1); System.out.println(b2); System.out.println(b3); System.out.println(b4); } }