Page 1 of 4
Java - Boolean class
Java Boolean Class
The Java 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.
Boolean Class Declaration in Java
Following is the declaration for java.lang.Boolean class −
public final class Boolean
extends Object
implements Serializable, Comparable<Boolean>
Learn Java in-depth with real-world projects through our Java certification course. Enroll and
become a certified expert to boost your career.
Boolean Class Fields
Following are the fields for java.lang.Boolean class −
static Boolean FALSE − This is the Boolean object corresponding to the primitive value
false.
static Boolean TRUE − This is the Boolean object corresponding to the primitive value
true.
static Class<Boolean> TYPE − This is the Class object representing the primitive type
boolean.
Boolean Class Constructors
Sr.No. Constructor & Description
Boolean(boolean value)
1
This allocates a Boolean object representing the value argument.
Boolean(String s)
2 This allocates a Boolean object representing the value true if the string argument is
not null and is equal, ignoring case, to the string "true".
100% Placement Assistance. Weekdays
Open
& Weekend Batches. Enroll now.
Page 2 of 4
Boolean Class Methods
Sr.No. Method & Description
boolean booleanValue()
1
This method returns the value of this Boolean object as a boolean primitive.
int compareTo(Boolean b)
2
This method compares this Boolean instance with another.
boolean equals(Object obj)
3 This method 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)
4 This method returns true if and only if the system property named by the argument
exists and is equal to the string "true".
int hashCode()
5
This method returns a hash code for this Boolean object.
int hashCode(boolean value)
6 This method returns a hash code for a given boolean value. It is compatible with
Boolean.hashCode().
static boolean logicalAnd(boolean a, boolean b)
7 This method returns the result of applying the logical AND operator to the specified
boolean operands.
static boolean logicalOr(boolean a, boolean b)
8 This method returns the result of applying the logical OR operator to the specified
boolean operands.
static boolean logicalXor(boolean a, boolean b)
9 This method returns the result of applying the logical XOR operator to the specified
boolean operands.
static boolean parseBoolean(String s)
10
This method parses the string argument as a boolean.
String toString()
11
This method returns a String object representing this Boolean's value.
static String toString(boolean b)
12
This method returns a String object representing the specified boolean.
Page 3 of 4
static Boolean valueOf(boolean b)
13
This method returns a Boolean instance representing the specified boolean value.
static Boolean valueOf(String s)
14
This method returns a Boolean with a value represented by the specified string.
Methods Inherited
This class inherits methods from the following classes −
java.lang.Object
Example of Java Boolean Class
The following example shows the usage of some important methods provided by Boolean class.
Open Compiler
package com.tutorialspoint;
public class BooleanDemo {
public static void main(String[] args) {
// create 2 Boolean objects b1, b2
Boolean b1, b2;
// assign values to b1, b2
b1 = Boolean.valueOf(true);
b2 = Boolean.valueOf(false);
// create an int res
int res;
// compare b1 with b2
res = b1.compareTo(b2);
String str1 = "Both values are equal ";
String str2 = "Object value is true";
String str3 = "Argument value is true";
if( res == 0 ) {
System.out.println( str1 );
} else if( res > 0 ) {
Page 4 of 4
System.out.println( str2 );
} else if( res < 0 ) {
System.out.println( str3 );
}
}
}
Output
Let us compile and run the above program, this will produce the following result −
Object value is true