Test with Boolean Wrapper Class
In the previous step, we worked with the primitive boolean
type. Java also has a corresponding wrapper class called Boolean
. Wrapper classes provide a way to use primitive data types as objects. This is particularly useful when working with collections or when you need to represent a boolean value that might be null
.
The Boolean
class has two predefined objects for the boolean values: Boolean.TRUE
and Boolean.FALSE
. These are constant objects representing the boolean values true
and false
, respectively.
When working with Boolean
objects, you can still use the equality operator ==
to compare them. However, it's important to understand how ==
works with objects. For objects, ==
checks if the two variables refer to the exact same object in memory, not just if they have the same value.
Let's modify our program to use the Boolean
wrapper class and see how the equality operator behaves.
-
Open the HelloJava.java
file in the WebIDE editor.
-
Replace the code with the following:
public class HelloJava {
public static void main(String[] args) {
Boolean isJavaFunObject = Boolean.TRUE;
if (isJavaFunObject == Boolean.TRUE) {
System.out.println("Java is fun (using Boolean.TRUE)!");
} else {
System.out.println("Java is not fun (using Boolean.TRUE).");
}
Boolean anotherBooleanObject = true; // Autoboxing
if (anotherBooleanObject == Boolean.TRUE) {
System.out.println("Another boolean object is true!");
} else {
System.out.println("Another boolean object is not true.");
}
}
}
Let's look at the changes:
Boolean isJavaFunObject = Boolean.TRUE;
: We declare a variable of type Boolean
and assign it the constant Boolean.TRUE
.
if (isJavaFunObject == Boolean.TRUE)
: We use the equality operator ==
to compare our Boolean
object with the Boolean.TRUE
constant. Since isJavaFunObject
is assigned Boolean.TRUE
, they refer to the same object, so this condition will be true
.
Boolean anotherBooleanObject = true;
: This line demonstrates "autoboxing". Java automatically converts the primitive boolean
value true
into a Boolean
object.
if (anotherBooleanObject == Boolean.TRUE)
: We again use ==
to compare anotherBooleanObject
with Boolean.TRUE
. Due to how autoboxing works and Java's caching of Boolean
values, for the values true
and false
, the autoboxed Boolean
objects often refer to the same cached instances as Boolean.TRUE
and Boolean.FALSE
. Therefore, this condition will also likely be true
.
-
Save the file.
-
Compile the program:
javac HelloJava.java
-
Run the program:
java HelloJava
You should see the output:
Java is fun (using Boolean.TRUE)!
Another boolean object is true!
This confirms that using ==
with Boolean.TRUE
works as expected in these cases because the variables are likely referencing the same underlying Boolean.TRUE
object.
However, relying on ==
for comparing Boolean
objects can be risky in more complex scenarios, especially if the Boolean
objects are created in different ways or come from different sources. A safer and recommended way to compare Boolean
objects for equality of value is to use the .equals()
method.
Let's modify the code to use .equals()
.
-
Open HelloJava.java
in the editor.
-
Change the if
statements to use .equals()
:
public class HelloJava {
public static void main(String[] args) {
Boolean isJavaFunObject = Boolean.TRUE;
if (isJavaFunObject.equals(Boolean.TRUE)) {
System.out.println("Java is fun (using equals)!");
} else {
System.out.println("Java is not fun (using equals).");
}
Boolean anotherBooleanObject = true; // Autoboxing
if (anotherBooleanObject.equals(Boolean.TRUE)) {
System.out.println("Another boolean object is true (using equals)!");
} else {
System.out.println("Another boolean object is not true (using equals).");
}
}
}
We replaced == Boolean.TRUE
with .equals(Boolean.TRUE)
. The .equals()
method compares the value of the objects, not their memory location.
-
Save the file.
-
Compile the program:
javac HelloJava.java
-
Run the program:
java HelloJava
You should see the output:
Java is fun (using equals)!
Another boolean object is true (using equals)!
Using .equals()
is the standard and safest way to compare Boolean
objects for value equality.
In summary, while ==
might work for comparing Boolean
objects with Boolean.TRUE
due to caching, the .equals()
method is the preferred and more reliable way to check if a Boolean
object represents the value true
.