In java.lang package, there is a wrapper class Boolean. A value of the primitive type boolean is wrapped in an object by the Boolean class. An object of type Boolean has a single field with the type boolean.
Constructors for creating Boolean object:
Boolean(boolean value)
Boolean(String s)
Methods of Boolean Wrapper class:
Methods | Description |
---|---|
boolean booleanValue() | Returns a primitive boolean value from a Boolean wrapper object. |
int compareTo(Boolean b) | -If the invoking Boolean object has the same value as b, it returns zero. -Returns 1 if the invoking object is true and b is false. –Returns -1 if the invoking object is false and b is true. |
boolean equals(Object ob) | Returns true if the invoking Boolean object has the same value as ob, otherwise false. |
static boolean parseBoolean(String s) | Returns true if the String contains true (in any case), otherwise false. |
static boolean valueOf(boolean b) | Returns a Boolean object, equivalent to b. |
static boolean valueOf(String s) | Returns a Boolean instance representing the specified Boolean value or string value. |
hashCode() | Returns the Boolean object’s hash code. |
logicalAnd() | Returns the outcome of performing a logical AND operation on the given boolean operands. |
logicalOr() | Returns the outcome of performing a logical OR operation on the given boolean operands. |
logicalXor() | Returns the outcome of performing a logical XOR operation on the given boolean operands. |
Example: Illustrating the use of various methods
public class Coderz
{
public static void main(String[] args)
{
Boolean b1 = true;
System.out.println("Boolean value = " + b1);
//prints the hash code of the boolean value
System.out.println("Hash Code for boolean value = " + b1.hashCode());
//converting boolean value to String
String str = b1.toString();
System.out.println("String value = " + str);
System.out.println("Hash Code for String Value = " + str.hashCode());
// will return a boolean instance corresponding to Boolean b1
Boolean b2 = Boolean.valueOf(b1);
System.out.println("valueOf() method will return = " + b2);
boolean val1 = false;
for (int i = 0; i < 6; i++) {
if (i == 5) {
System.setProperty("val1", "true");
break;
}
}
boolean b3 = Boolean.getBoolean("val1");
System.out.println("value of val1 is " + b3);
}
}
Output:
Boolean value = true
Hash Code for boolean value = 1231
String value = trueHash Code for String Value = 3569038
valueOf() method will return = true
value of val1 is true
Example: comparing values
class Coderz
{
public static void main(String[] ar)
{
Boolean b1 = new Boolean("true"); //Constructor accepting String value
Boolean b2 = new Boolean(true); //Constructor accepting primitive boolean value
System.out.println("Value in b1 = "+b1);
System.out.println("Value in b2 = "+b2);
System.out.println("Invoking "+b1+" to compare with "+b2+" : "+ b1.compareTo(b2));
Boolean b3 = new Boolean("true");
Boolean b4 = new Boolean(false);
System.out.println("Value in b3 = "+b3);
System.out.println("Value in b4 = "+b4);
System.out.println("Invoking "+b3+" to compare with "+b4+" : "+ b3.compareTo(b4));
System.out.println("Invoking "+b4+" to compare with "+b3+" : "+ b4.compareTo(b3));
}
}
Output:
Value in b1 = true
Value in b2 = true
Invoking true to compare with true : 0
Value in b3 = true
Value in b4 = false
Invoking true to compare with false : 1
Invoking false to compare with true : -1
Note: also read about the Double class in Java
Follow Me
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment