Topics Index

Exploring java.lang > Primitive Type Wrappers >
Siva Nookala - 14 Apr 2016
Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field whose type is boolean.

In addition, this class provides many methods for converting a boolean to a String and a String to a boolean, as well as other constants and methods useful when dealing with a boolean.
Boolean Constructors :
Boolean(boolean value)Allocates a Boolean object representing the value argument.
Boolean(String s)Allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string "true".

Boolean Methods :
boolean booleanValue()Returns the value of this Boolean object as a boolean primitive.
static int compare(boolean x, boolean y)Compares two boolean values.
int compareTo(Boolean b)Compares this Boolean instance with another.
boolean equals(Object obj)Returns true if and only if the argument is not null and is a Boolean object that represents the same boolean value as this object.
static boolean getBoolean(String name)Returns true if and only if the system property named by the argument exists and is equal to the string "true".
int hashCode()Returns a hash code for this Boolean object.
static boolean parseBoolean(String s)Parses the string argument as a boolean.
String toString()Returns a String object representing this Boolean's value.
static String toString(boolean b)Returns a String object representing the specified boolean.
static Boolean valueOf(boolean b)Returns a Boolean instance representing the specified boolean value.
static Boolean valueOf(String s)Returns a Boolean with a value represented by the specified String.

class BooleanTest
    public static void main(String arg[])
        Boolean a = new Boolean(true);
        Boolean b = new Boolean(false);
        Boolean c = new Boolean(false);
        System.out.println("Boolean value of a : " + a.booleanValue());
        System.out.println("Comparison of b and c using compare : " +, c));
        System.out.println("CompareTo of a and c : " + a.compareTo(c)); // LINE A
        System.out.println("Equals of b and c : " + b.equals(c));
        System.setProperty("Isboolean", "true"); // LINE B
        System.out.println("Get Boolean from Isboolean : " + Boolean.getBoolean("Isboolean"));
        System.out.println("Hash code for Boolean : " + a.hashCode()); // LINE C
        System.out.println("ParseBoolean converts String toboolean : " + Boolean.parseBoolean("true"));
        System.out.println("Boolean is converted into String : " + b.toString());
        System.out.println("Boolean is converted into String : " + Boolean.toString(a));
        System.out.println("Converts Boolean into Boolean : " + Boolean.valueOf(a));
        System.out.println("Converts String into Boolean : " + Boolean.valueOf("true"));    

Boolean value of a : true
Compare of b and c : 0
CompareTo of a and c : 1
Equals of b and c : true
Get Boolean from property : true
Hash code for Boolean : 1231
ParseBoolean converts String : true
Boolean is converted into String : false
Boolean is converted into String : true
Converts Boolean into Boolean : true
Converts String into Boolean : true


In this program, all the Boolean methods used to obtain Boolean value.

  • At LINE A, interchange the variables a, c in compareTo method and see the output difference.
  • At LINE B, in place of "true" put "false" and see the output difference.
  • At LINE C, replace a by b and see the hash code difference.

Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.