Java - Boolean Wrapper Class

What is Boolean Wrapper Class?

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.

You 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 b11True = new Boolean(true);
    Boolean b21True = new Boolean("true");
    Boolean b31True = new Boolean("tRuE");
    Boolean b41False = new Boolean("false");
    Boolean b51False = new Boolean("how is this"); // false

    // Using the factory methods
    Boolean b12True = Boolean.valueOf(true);
    Boolean b22True = Boolean.valueOf("true");
    Boolean b32True = Boolean.valueOf("tRuE");
    Boolean b42False = Boolean.valueOf("false");
    Boolean b52False = Boolean.valueOf("from"); // false

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

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

    // Using constants
    Boolean bcTrue = Boolean.TRUE;
    Boolean bcFalse = Boolean.FALSE;

    // Printing some Boolean objects
    System.out.println("bcTrue = " + bcTrue);
    System.out.println("bcFalse = " + bcFalse);

  }/*from  w ww.  jav  a 2 s  . c  o m*/


Related Topic