The Boolean class in Java is a wrapper class that encapsulates a boolean value. It provides several methods to perform various operations on boolean values. In this blog post, we will explore each of the methods provided by the Boolean class and provide complete program examples for each method.
boolean booleanValue()
The booleanValue() method returns the value of the Boolean object as a boolean primitive.
public class Main {
public static void main(String[] args) {
Boolean bool = Boolean.TRUE;
boolean value1 = bool.booleanValue();
bool = Boolean.FALSE;
// Remove unboxing, it's not necessary
boolean value2 = bool;
// Output: true
System.out.println(value1);
// Output: false
System.out.println(value2);
}
}
static int compare(boolean x, boolean y)
The compare() method compares two boolean values. It returns 0 if the values are equal, 1 if the first value is true and the second value is false, and -1 if the first value is false and the second value is true.
public class Main {
public static void main(String[] args) {
// Output: -1
printComparisonResults(false, true);
// Output: 0
printComparisonResults(true, true);
// Output: 1
printComparisonResults(true, false);
}
static void printComparisonResults(boolean boolean1, boolean boolean2) {
int result = Boolean.compare(boolean1, boolean2);
System.out.println(result);
}
}
int compareTo(Boolean b)
The compareTo() method compares the value of the Boolean object with the specified Boolean object. It returns 0 if the values are equal, 1 if the current value is true and the specified value is false, and -1 if the current value is false and the specified value is true.
public class Main {
public static void main(String[] args) {
// Output: -1
printComparisonResults(Boolean.valueOf(false), Boolean.valueOf(true));
// Output: 0
// Boxing removed, not necessary
printComparisonResults(false, false);
// Output: 1
printComparisonResults(true, false);
}
static void printComparisonResults(Boolean boolean1, Boolean boolean2) {
int result = boolean1.compareTo(boolean2);
System.out.println(result);
}
}
boolean equals(Object obj)
The equals() method compares the Boolean object with the specified object. It returns true if the objects are equal, i.e., if they are both Boolean objects with the same boolean value.
public class Main {
public static void main(String[] args) {
Boolean boolean1 = Boolean.TRUE;
Boolean boolean2 = Boolean.TRUE;
boolean result = boolean1.equals(boolean2);
// Output: true
System.out.println(result);
}
}
static boolean getBoolean(String name)
The getBoolean() method returns the boolean value of the system property with the specified name. If the property does not exist or cannot be parsed as a boolean, it returns false.
public class Main {
public static void main(String[] args) {
boolean value = Boolean.getBoolean("java.version");
// Output: false
System.out.println(value);
}
}
int hashCode()
The hashCode() method returns the hash code value for the Boolean object.
public class Main {
public static void main(String[] args) {
Boolean bool = Boolean.TRUE;
int hashCode = bool.hashCode();
// Output: 1231
System.out.println(hashCode);
}
}
static int hashCode(boolean value)
The hashCode() method returns the hash code value for the specified boolean value.
public class Main {
public static void main(String[] args) {
boolean value = true;
int hashCode = Boolean.hashCode(value);
// Output: 1231
System.out.println(hashCode);
}
}
static boolean logicalAnd(boolean a, boolean b)
The logicalAnd() method computes the logical AND of two boolean values. Both values need to be true for an expression to evaluate to true.
public class Main {
public static void main(String[] args) {
// Output: false
printLogicalAndResults(true, false);
// Output: true
printLogicalAndResults(true, true);
// Output: false
printLogicalAndResults(false, true);
// Output: false
printLogicalAndResults(false, false);
}
static void printLogicalAndResults(Boolean boolean1, Boolean boolean2) {
boolean result = Boolean.logicalAnd(boolean1, boolean2);
System.out.println(result);
}
}
static boolean logicalOr(boolean a, boolean b)
The logicalOr() method computes the logical OR of two boolean values. Either of the values need to be true for an expression to evaluate to true.
public class Main {
public static void main(String[] args) {
// Output: true
printLogicalOrResults(true, false);
// Output: true
printLogicalOrResults(true, true);
// Output: true
printLogicalOrResults(false, true);
// Output: false
printLogicalOrResults(false, false);
}
static void printLogicalOrResults(Boolean boolean1, Boolean boolean2) {
boolean result = Boolean.logicalOr(boolean1, boolean2);
System.out.println(result);
}
}
static boolean logicalXor(boolean a, boolean b)
The logicalXor() method computes the logical XOR of two boolean values. Only one value needs to be true for an expression to evaluate to true, only one!
public class Main {
public static void main(String[] args) {
// Output: true
printLogicalXorResults(true, false);
// Output: false
printLogicalXorResults(true, true);
// Output: true
printLogicalXorResults(false, true);
// Output: false
printLogicalXorResults(false, false);
}
static void printLogicalXorResults(Boolean boolean1, Boolean boolean2) {
boolean result = Boolean.logicalXor(boolean1, boolean2);
System.out.println(result);
}
}
static boolean parseBoolean(String s)
The parseBoolean() method parses the specified string argument as a boolean. It returns true if the string is not null and is equal, ignoring case, to the string “true”.
public class Main {
public static void main(String[] args) {
String str = "true";
boolean value = Boolean.parseBoolean(str);
// Output: true
System.out.println(value);
}
}
String toString()
The toString() method returns a string representation of the Boolean object.
public class Main {
public static void main(String[] args) {
Boolean bool = Boolean.valueOf(true);
String str = bool.toString();
// Output: true
// As a string, can perform string operations on this
System.out.println(str);
// Output: TRUE
// String converted to uppercase
System.out.println(str.toUpperCase());
}
}
static String toString(boolean b)
The toString() method returns a string representation of the specified boolean value.
public class Main {
public static void main(String[] args) {
boolean value = true;
String str = Boolean.toString(value);
// Output: true
// As a string, can perform string operations on this
System.out.println(str);
// Output: TRUE
// String converted to uppercase
System.out.println(str.toUpperCase());
}
}
static Boolean valueOf(boolean b)
The valueOf() method returns a Boolean instance representing the specified boolean value.
public class Main {
public static void main(String[] args) {
boolean value = true;
Boolean bool = Boolean.valueOf(value);
// Output: true
System.out.println(bool);
// Boxing can be removed, code works just fine
bool = value;
// Output: true
System.out.println(bool);
}
}
static Boolean valueOf(String s)
The valueOf() method returns a Boolean instance representing the boolean value true if the specified string is not null and is equal, ignoring case, to the string “true”. Otherwise, it returns a Boolean instance representing the boolean value false.
public class Main {
public static void main(String[] args) {
String str = "true";
// Convert string "true" to boolean
Boolean bool = Boolean.valueOf(str);
System.out.println(bool);
}
}
Conclusion
We explored the Java Boolean class and its methods. We discussed the purpose of each method and provided complete program examples to demonstrate their usage. The Boolean class and its methods are valuable tools for working with boolean values in Java programs, allowing for efficient and convenient boolean manipulation.
I hope you found this information informative. If you would like to receive more content, please consider subscribing to our newsletter!