Java Boolean Class
The Boolean class wraps a value of the Boolean primitive in an object. Its object contains only a single field whose type is Boolean.
Boolean Methods:
This class contains several different methods and other constants for converting Boolean to a string and vice versa and also many other methods which are useful dealing with a Boolean.
| Methods | Description |
| booleanValue() | It returns a Boolean value for the Boolean arguments. |
| compare() | It compares the two Boolean arguments. |
| compareTo() | It compares the given Boolean instance with another. |
| equals() | It returns a Boolean value true only if the specified argument is not null and the Boolean argument represents the same value as the object. |
| getBoolean() | It returns a Boolean value true if and only if the system property name is not null and is equal to ”true”. |
| hashCode() | It returns a hash code for the specified Boolean object. |
| logicalAnd() | It returns the result after implementing logical AND operation on the Boolean arguments. |
| logicalOr() | It returns the result after implementing logical OR operation on the specified Boolean arguments. |
| logicalXor() | It returns the result after implementing logical XOR operation on the Boolean arguments. |
| parseBoolean() | It returns a Boolean value represented by the string argument by parsing the string argument as a Boolean. |
| toString() | It returns a String instance representing the specified Boolean’s value. |
| valueOf() | It returns a Boolean instance representing the given string or Boolean value. |
Example 1:
public class JavaBooleanClassExample1 {
static int i=1;
public static void main(String[] args) {
Boolean b1= true;
Boolean b2=true;
//assigning boolean value of b1 to b3
Boolean b3= b1.booleanValue();
System.out.println(i++ + ". booleanValue method will return : "+b3);
//check whether b1 and b2 are equal or not
Boolean b4 = b1.equals(b2);
System.out.println(i++ + ". equals method will return : "+b4);
// logicalOr() with return the same result as OR operator
Boolean b5 = Boolean.logicalOr(b1,b2);
System.out.println(i++ + ". Logical And will return: "+b5);
}
}
Output:
1. booleanValue method will return : true 2. equals method will return : true 3. Logical And will return: true
Example 2:
public class JavaBooleanClassExample2 {
static int i=1;
public static void main(String[] args) {
Boolean b1= true;
Boolean b2=true;
//return the hash code for the boolean value
int b3= b1.hashCode();
System.out.println(i++ + ". Hash code of"+ b1+" : "+b3);
//parses the string argument to boolean
String str = "false";
Boolean b4= Boolean.parseBoolean(str);
System.out.println(i++ + ". parseBoolean method will return : "+b4);
// returns a String instance representing the specified Boolean’s value
String b5 = Boolean.toString(b1);
System.out.println(i++ + ". toString will return : "+b5);
// returns a Boolean instance representing the specified boolean value.
Boolean b6 = Boolean.valueOf(b1);
System.out.println(i++ + ". valueOf method will return : "+b5);
}
}
Output:
1. Hash code oftrue : 1231 2. parseBoolean method will return : false 3. toString will return : true 4. valueOf method will return : true
