Convert Java String to boolean

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"));
  }
}
Output: String ‘true’ to boolean: true String ‘TRUE’ to boolean: true String ‘True’ to boolean: true String ‘false’ to boolean: false String ‘False’ to boolean: false String ‘abc’ to boolean: false String ‘TruE’ to boolean: 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"));

  }
}
Output: String ‘true’ to boolean: true String ‘TRUE’ to boolean: true String ‘True’ to boolean: true String ‘false’ to boolean: false String ‘False’ to boolean: false String ‘abc’ to boolean: false String ‘TruE’ to boolean: 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);
  }
}
Output: true true false false
 
To be proficient in working with Java strings and boolean values, it’s crucial to understand how to convert between the two. For more details on converting a boolean to a Java string, refer to our related tutorial Convert boolean to String in Java. By mastering both conversions, you will be able to handle strings and booleans effectively in your programs.
 
That’s it!

Leave a Reply

Your email address will not be published. Required fields are marked *