Wrapper classes in Java help perform object operations that we normally cannot perform with primitive types. This post will discuss one such wrapper class, which wraps the boolean primitive value and helps us perform object operations whenever needed. Here, we are talking about the Boolean wrapper class of Java.
The Boolean class is present in java.lang package.
Declaration of the Boolean wrapper class
public final class Boolean implements java.io.Serializable,
Comparable<Boolean>, Constable
How to make an object of the Boolean Wrapper class?
There are several ways of creating the objects of the Boolean wrapper class –
- Boolean(boolean value) constructor
- Boolean(String value) constructor
- valueOf(boolean b) method
- valueOf(String s) method
- Boolean constants
Let’s look at all of the ways one by one.
– Boolean(boolean value) constructor
- Method declaration – public Boolean(boolean value)
- What does it do? It will create an instance of the Boolean wrapper class representing the value of the boolean primitive value passed in its arguments.
This constructor has been marked as deprecated since Java 9, and thus it’s not recommended to use it anymore.
public class Codekru {
public static void main(String[] args) {
Boolean b1 = new Boolean(false);
System.out.println("b1 value: " + b1);
}
}
Internal implementation of the Boolean(boolean value) constructor
public Boolean(boolean value) {
this.value = value;
}
– Boolean(String value) constructor
- Method declaration – public Boolean(String s)
- What does it do? It will also create the instance or object of the Boolean class. This constructor takes a string argument and parses it to assign a corresponding boolean value to the object.
Note: This constructor is also marked as deprecated and is not recommended to use anymore.
Below are the boolean values that will be assigned on passing various strings.
"true" string -> true boolean value
"false" string -> false boolean value
"abc" string -> false boolean value
Passing a string other than “true” will result in a false boolean value.
public class Codekru {
public static void main(String[] args) {
Boolean b1 = new Boolean("true");
Boolean b2 = new Boolean("false");
Boolean b3 = new Boolean("qwerty");
System.out.println("b1 value: " + b1);
System.out.println("b2 value: " + b2);
System.out.println("b3 value: " + b3);
}
}
Output –
b1 value: true
b2 value: false
b3 value: false
– valueOf(boolean b) method
- Method declaration – public static Boolean valueOf(boolean b)
- What does it do? It exists since Java 1.4 and is recommended to use for making an object of the Boolean wrapper class. This method will take a boolean value into the argument and is more likely to yield better space and time performance than the traditional parameterized constructors we discussed above.
public static void main(String[] args) {
Boolean b1 = Boolean.valueOf(true);
System.out.println("b1 value: " + b1);
}
}
Output –
b1 value: true
– valueOf(String s) method
- Method declaration – public static Boolean valueOf(String s)
- What does it do? It will take a string argument, preferably “true” and “false”, and convert them into their corresponding Boolean instances.
This method provides better space and time performance than the Boolean(String value) constructor and is recommended.
public class Codekru {
public static void main(String[] args) {
Boolean b1 = Boolean.valueOf("true");
Boolean b2 = Boolean.valueOf("false");
Boolean b3 = Boolean.valueOf("qwerty");
System.out.println("b1 value: " + b1);
System.out.println("b2 value: " + b2);
System.out.println("b3 value: " + b3);
}
}
Output –
b1 value: true
b2 value: false
b3 value: false
– Boolean constants
We can also use the constants provided by the Boolean class to make the instances.
public class Codekru {
public static void main(String[] args) {
Boolean b1 = Boolean.TRUE;
Boolean b2 = Boolean.FALSE;
System.out.println("b1 value: " + b1);
System.out.println("b2 value: " + b2);
}
}
Output –
b1 value: true
b2 value: false
These constants internally use the parameterized constructors to make objects, and Java is deprecating the use of those constructors. So, making the object this way is not recommended, either.
The most efficient way of making objects of a Boolean wrapper class is by using valueOf() methods.
Comparing and caching in Boolean Class’s Objects
How we can compare and check the equality of two Boolean class objects?
We can compare two Boolean class objects using the equals() method or the == operator.
- == operator compares the references of two objects, not the value they hold.
- whereas, the equals() method compares based on the primitive values held by the objects. The references may or may not be the same.
Let’s look at the example side by side.
public class Codekru {
public static void main(String[] args) {
Boolean b1 = new Boolean(false);
Boolean b2 = new Boolean(false);
if (b1 == b2) {
System.out.println("Both references are equal");
} else {
System.out.println("Both aren't equal");
}
}
}
Output –
Both aren't equal
public class Codekru {
public static void main(String[] args) {
Boolean b1 = new Boolean(false);
Boolean b2 = new Boolean(false);
if (b1.equals(b2)) {
System.out.println("Both are equal");
} else {
System.out.println("Both aren't equal");
}
}
}
Output –
Both are equal
== operator returned false because it only compares the references, not the values they hold. And b1 and b2 are two different instances as we have made them using the new keyword.
Caching in Boolean Class
We discussed using valueOf() methods to create objects because it provides better space and time performance. But how?
Any objects created using the valueOf() methods are cached, and new objects are taken from the cached references. Thus, no new objects will be created.
public class Codekru {
public static void main(String[] args) {
Boolean b1 = Boolean.valueOf(false);
Boolean b2 = Boolean.valueOf(false);
if (b1 == b2) {
System.out.println("Both references are equal");
} else {
System.out.println("Both aren't equal");
}
}
}
Output –
Both references are equal
We can see that the == operator returned true, which means that both references are equal.
This is why we should create the objects using the valueOf() method.
Boolean wrapper class methods
The boolean wrapper class has provided us with many methods, and some of them are listed below –
Method Name | What does it do? |
---|---|
public boolean booleanValue() |
It returns the boolean primitive value of the instance. |
public int compareTo(Boolean b) |
It compares the current boolean instance with the other. |
public static int compare(boolean x, boolean y) |
It compares two boolean values |
public static boolean getBoolean(String name) |
It returns the boolean value of the system property passed in the arguments. |
public static boolean logicalAnd(boolean a, boolean b) |
It applies the logical AND operator to the boolean values passed in the arguments. |
public static boolean logicalOr(boolean a, boolean b) |
It applies the logical OR operator to the boolean values passed in the arguments. |
public static boolean logicalXor(boolean a, boolean b) |
It applies the logical XOR operator to the boolean values passed in the arguments. |
public static boolean parseBoolean(String s) |
It parses the string as a boolean |
public static String toString(boolean b) |
It returns a string object representing the boolean value passed in the argument. |
public static Boolean valueOf(boolean b) |
It returns a Boolean instance representing the boolean value passed in the argument. |
public static Boolean valueOf(String s) |
It returns a Boolean instance representing the string value passed in the argument. |
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.