Boolean is a class available in the java.lang package, and it is also an immutable class.
java.lang.Boolean:
public final class java.lang.Boolean implements java.io.Serializable, java.lang.Comparable<java.lang.Boolean> {
public static final java.lang.Boolean TRUE;
public static final java.lang.Boolean FALSE;
public static final java.lang.Class<java.lang.Boolean> TYPE;
publicjava.lang.Boolean(boolean);
publicjava.lang.Boolean(java.lang.String);
public static booleanparseBoolean(java.lang.String);
publicbooleanbooleanValue();
public static java.lang.BooleanvalueOf(boolean);
public static java.lang.BooleanvalueOf(java.lang.String);
public static java.lang.StringtoString(boolean);
publicjava.lang.StringtoString();
publicinthashCode();
public static inthashCode(boolean);
publicboolean equals(java.lang.Object);
public static booleangetBoolean(java.lang.String);
publicintcompareTo(java.lang.Boolean);
public static int compare(boolean, boolean);
public static booleanlogicalAnd(boolean, boolean);
public static booleanlogicalOr(boolean, boolean);
public static booleanlogicalXor(boolean, boolean);
publicintcompareTo(java.lang.Object);
static {};
}
Important Constructor of java.lang.Boolean:
1. public java.lang.Boolean(boolean): It creates a new object of the java.lang.Boolean class, representing the specified boolean value.
2. public java.lang.Boolean(java.lang.String): It creates an object of the java.lang.Boolean class, which wraps the specified boolean value provided as a string argument.
When we pass a null value or a string literal that is not equal to 'true', the result is false.
publicclass JTC {
publicstaticvoid main(String[] args) {
Boolean boolean1 = newBoolean(true);
Boolean boolean2 = newBoolean(false);
System.out.println("boolean1 :- " + boolean1);
System.out.println("boolean2 :- " + boolean2);
Boolean boolean3 = newBoolean("true");
Boolean boolean4 = newBoolean("false");
Boolean boolean5 = newBoolean(null);
Boolean boolean6 = newBoolean("abc");
System.out.println("boolean3 :- " + boolean3);
System.out.println("boolean4 :- " + boolean4);
System.out.println("boolean5 :- " + boolean5);
System.out.println("boolean6 :- " + boolean6);
}
}
boolean1 :- true boolean2 :- false boolean3 :- true boolean4 :- false boolean5 :- false boolean6 :- false
Important Member of java.lang.Boolean:
1. public static final java.lang.Boolean TRUE: It is a final static reference variable of Boolean type that contains an object of the java.lang.Boolean class, wrapping the 'true' boolean value.
2. public static final java.lang.Boolean FALSE: It is a final static reference variable of Boolean type that holds a Boolean-type object wrapping the 'false' value.
3. public static final java.lang.Class<java.lang.Boolean> TYPE: It is a final static reference variable of type java.lang.Class that holds the reference to the java.lang.Class object representing the boolean primitive type.
4. public static booleanparseBoolean(java.lang.String): It is primarily used to parse string literals into boolean-type values. It returns 'true' when the string literal is equal to, ignoring case, 'true'; otherwise, it returns 'false'.
5. public booleanbooleanValue(): It returns a boolean value that is the same as the boolean value of the current working object.
6. public static java.lang.BooleanvalueOf(boolean): It returns a new object of the java.lang.Boolean class, which holds the same boolean value as the specified argument.
7. public static java.lang.BooleanvalueOf(java.lang.String): It returns a new object of the java.lang.Boolean class, which wraps the same boolean value as the specified String. It returns 'true' if the specified String argument is equal, ignoring case, to 'true'; otherwise, it returns 'false'.
8. public inthashCode(): The hashCode() method is overridden in the java.lang.Boolean class to return the hash code of the Boolean object. It returns 1231 for a true Boolean object and 1237 for a false Boolean object.
9. public boolean equals(java.lang.Object): The equals() method is overridden in the java.lang.Boolean class, and it returns true if both Boolean objects have the same boolean value; otherwise, it returns false.
10. publicintcompareTo(java.lang.Object): The compareTo() method is an abstract method of the java.lang.Comparable interface, and it is overridden in the java.lang.Boolean class. It returns 0 when the current working object and the argument object have the same boolean value, 1 when the current working object contains true and the argument object wraps false, and -1 when the current working object has false and the argument object has true.
publicclass JTC {
publicstaticvoid main(String[] args) {
System.out.println("Boolean.TRUE :- " + Boolean.TRUE);
System.out.println("Boolean.FALSE :- " + Boolean.FALSE);
System.out.println("Boolean.TYPE :- " + Boolean.TYPE);
booleanb1 = Boolean.parseBoolean("true");
System.out.println("b1 :- " + b1);
booleanb2 = Boolean.parseBoolean("false");
System.out.println("b2 :- " + b2);
booleanb3 = Boolean.parseBoolean("abc");
System.out.println("b3 :- " + b3);
booleanb4 = Boolean.parseBoolean(null);
System.out.println("b4 :- " + b4);
Boolean boolean1 = newBoolean(true);
booleanb5 = boolean1.booleanValue();
System.out.println("b5 :- " + b5);
Boolean boolean2 = Boolean.valueOf(true);
Boolean boolean3 = Boolean.valueOf(false);
System.out.println("boolean2 :- " + boolean2);
System.out.println("boolean3 :- " + boolean3);
Boolean boolean4 = Boolean.valueOf("true");
Boolean boolean5 = Boolean.valueOf("false");
Boolean boolean6 = Boolean.valueOf("True");
Boolean boolean7 = Boolean.valueOf("False");
Boolean boolean8 = Boolean.valueOf(null);
Boolean boolean9 = Boolean.valueOf("abc");
System.out.println("boolean4 :- " + boolean4);
System.out.println("boolean5 :- " + boolean5);
System.out.println("boolean6 :- " + boolean6);
System.out.println("boolean7 :- " + boolean7);
System.out.println("boolena8 :- " + boolean8);
System.out.println("boolean9 :- " + boolean9);
System.out.println("true Boolean object :- " + boolean1.hashCode());
System.out.println("false Boolean object :- " + boolean3.hashCode());
System.out.println(boolean1.equals(boolean2));
System.out.println(boolean2.equals(boolean3));
System.out.println(boolean3.equals(null));
System.out.println("same boolean value :- " + boolean1.compareTo(boolean2));
System.out.println("true.compareTo(false) :- " + boolean1.compareTo(boolean3));
System.out.println("false.compareTo(true) :- " + boolean3.compareTo(boolean1));
}
}
Boolean.TRUE :- true Boolean.FALSE :- false Boolean.TYPE :- boolean b1 :- true b2 :- false b3 :- false b4 :- false b5 :- true boolean2 :- true boolean3 :- false boolean4 :- true boolean5 :- false boolean6 :- true boolean7 :- false boolena8 :- false boolean9 :- false true Boolean object :- 1231 false Boolean object :- 1237 true false false sameboolean value :- 0 true.compareTo(false) :- 1 false.compareTo(true) :- -1