Java Data Type Tutorial - Java Boolean Wrapper Class

An object of the Boolean class wraps a boolean.

The Boolean.TRUE and Boolean.FALSE are two constants of the Boolean type to represent boolean true and false values.

We can create a Boolean object using the constructors or the valueOf() factory method.

When parsing a string, this class treats "true" ignoring the case of all characters as the true and any other strings as the false.


The following code shows how to use the Boolean class.

public class Main {
  public static void main(String[] args) {
    // Using constructors
    Boolean b1True = new Boolean(true);
    Boolean b2True = new Boolean("true");
    Boolean b3True = new Boolean("tRuE");
    Boolean b4False = new Boolean("false");
    Boolean b5False = new Boolean("how  is this"); // false
/*from w  w w. ja v  a 2  s .c  om*/
    // Using the factory methods
    Boolean b6True = Boolean.valueOf(true);
    Boolean b7True = Boolean.valueOf("true");
    Boolean b8True = Boolean.valueOf("tRuE");
    Boolean b9False = Boolean.valueOf("false");
    Boolean b10False = Boolean.valueOf("how is this"); // false

    // Getting a boolean value from a Boolean object
    boolean bbTrue = b8True.booleanValue();

    boolean bTrue = Boolean.parseBoolean("true");
    boolean bFalse = Boolean.parseBoolean("This string evaluates to false");

    Boolean bcTrue = Boolean.TRUE;
    Boolean bcFalse = Boolean.FALSE;

    System.out.println("bcTrue = " + bcTrue);
    System.out.println("bcFalse = " + bcFalse);

The code above generates the following result.