The 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 class 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".
Example:
Boolean boolean1 = new Boolean(true);
Boolean boolean2 = new Boolean("boolean");
Boolean class Methods
The class diagram shows a list of APIs/Methods that Boolean class provides.
In this post, we will learn a few important methods of the Boolean wrapper class.
parseBoolean(String s)
Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".
Example:
private static void parseBoolean(){
// parsing different Strings
boolean b1 = Boolean.parseBoolean("True");
boolean b2 = Boolean.parseBoolean("TruE");
boolean b3 = Boolean.parseBoolean("False");
boolean b4 = Boolean.parseBoolean("FALSE");
boolean b5 = Boolean.parseBoolean("GeeksForGeeks");
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
System.out.println(b4);
System.out.println(b5);
}
booleanValue()
Returns the value of this Boolean object as a boolean primitive.
Example:
private static void booleanValue(){
// creating different Boolean objects
Boolean b1 = new Boolean("True");
Boolean b2 = new Boolean("False");
Boolean b3 = new Boolean("GeeksForGeeks");
// getting primitive boolean value
boolean b4 = b1.booleanValue();
boolean b5 = b2.booleanValue();
boolean b6 = b3.booleanValue();
System.out.println(b4);
System.out.println(b5);
System.out.println(b6);
}
valueOf(boolean b)
Returns a Boolean instance representing the specified boolean value. If the specified boolean value is true, this method returns Boolean.TRUE; if it is false, this method returns Boolean.FALSE.
Example:
private static void valueOf(){
// creating boolean variable
boolean b1 = true;
boolean b2 = false;
// getting Boolean objects from boolean variables
Boolean b3 = Boolean.valueOf(b1);
Boolean b4 = Boolean.valueOf(b2);
System.out.println(b3);
System.out.println(b4);
}
valueOf(String s)
Returns a Boolean with a value represented by the specified string. The Boolean returned represents a true value if the string argument is not null and is equal, ignoring case, to the string "true".
Example:
private static void valueOf1(){
// creating boolean variable using different Strings
Boolean b1 = Boolean.valueOf("true");
Boolean b2 = Boolean.valueOf("TRue");
Boolean b3 = Boolean.valueOf("False");
Boolean b4 = Boolean.valueOf("GeeksForGeeks");
Boolean b5 = Boolean.valueOf(null);
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
System.out.println(b4);
System.out.println(b5);
}
toString(boolean b)
Returns a String object representing the specified boolean.
Example:
private static void toStringTest(){
// creating boolean variable
boolean b1 = true;
boolean b2 = false;
// getting String value of the primitives boolean
String str1 = Boolean.toString(b1);
String str2 = Boolean.toString(b2);
System.out.println(str1);
System.out.println(str2);
}
toString()
Returns a String object representing this Boolean's value.
Example:
private static void toStringTest1(){
// creating different Boolean objects
Boolean b1 = new Boolean("True");
Boolean b2 = new Boolean("False");
Boolean b3 = new Boolean("GeeksForGeeks");
Boolean b4 = new Boolean(null);
// getting String value of Boolean objects
String str1 = b1.toString();
String str2 = b2.toString();
String str3 = b3.toString();
String str4 = b4.toString();
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println(str4);
}
hashCode()
Returns a hash code for this Boolean object.
Example:
private static void hashCodeTest(){
// creating different Boolean objects
Boolean b1 = new Boolean("True");
Boolean b2 = new Boolean("False");
Boolean b3 = new Boolean("TRue");
Boolean b4 = new Boolean(null);
System.out.println(b1.hashCode());
System.out.println(b2.hashCode());
System.out.println(b3.hashCode());
System.out.println(b4.hashCode());
}
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.
Example:
private static void equalsTest(){
// creating different Boolean objects
Boolean b1 = new Boolean("True");
Boolean b2 = new Boolean("False");
Boolean b3 = new Boolean("TrUe");
Boolean b4 = new Boolean(null);
// checking equality of Boolean objects
System.out.println(b1.equals(b2));
System.out.println(b2.equals(b4));
System.out.println(b1.equals(b3));
System.out.println(b1.equals(b4));
}
compareTo(Boolean b)
Compares this Boolean instance with another.
Example:
private static void comparetoTest(){
// creating different Boolean objects
Boolean b1 = new Boolean("True");
Boolean b2 = new Boolean("False");
Boolean b3 = new Boolean("TRue");
Boolean b4 = new Boolean(null);
//comparing b1,b2,b3,b4
System.out.println(b1.compareTo(b2));
System.out.println(b1.compareTo(b3));
System.out.println(b2.compareTo(b1));
System.out.println(b1.compareTo(b4));
System.out.println(b2.compareTo(b4));
// The following statement throws NullPointerExcetion
// System.out.println(b1.compareTo(null));
}
compare(boolean x, boolean y)
Compares two boolean values. The value returned is identical to what would be returned by:Boolean.valueOf(x).compareTo(Boolean.valueOf(y))
Example:
private static void compareTest(){
// creating boolean variable
boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
boolean b4 = false;
//comparing b1,b2,b3,b4
System.out.println(Boolean.compare(b1, b2));
System.out.println(Boolean.compare(b1, b3));
System.out.println(Boolean.compare(b2, b1));
System.out.println(Boolean.compare(b2, b4));
// The following statement throws NullPointerExcetion
// System.out.println(Boolean.compare(b1, null));
}
Reference
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course