1. Overview

In programming, some tasks seem deceptively simple like checking if two boolean values are equal. While this might sound trivial, there are several approaches to achieve this, each with unique implications on readability, functionality, and edge cases.

In this tutorial, we’ll explore how to check for equality between boolean values and when each method is appropriate.

2. Ways to Check Equality of Boolean Values

There are multiple ways to check for equality, each with its nuances and best-use scenarios. Understanding these methods can help us write clearer, safer, and more efficient code, especially when dealing with object references, handling null values, or integrating comparisons in more complex expressions.

In this section, we’ll explore various ways to check if two boolean values are equal.

2.1. Quick and Direct Comparison

The simplest and most intuitive way to check if two boolean values are equal is by using the == operator:

@Test
void givenPrimitiveBooleans_whenUsingDirectComparison_thenCompare() {
    boolean a = true;
    boolean b = true;
    assertTrue(a == b, 
      "Direct comparison of primitive booleans should be true when both are the same");

    a = true;
    b = false;
    assertFalse(a == b, 
      "Direct comparison of primitive booleans should be false when they differ");
}

In this example, the result will be true if both a and b have the same boolean value, and false otherwise. This works well for primitive boolean values (true or false) and is ideal when we’re sure both values are non-null and of type boolean.
Java’s == operator works naturally with primitive types, including boolean, as it checks value equality rather than reference equality. It’s also efficient, as it compares values directly. This approach is clear and performant for simple checks, making it the go-to choice for most cases.
However, the story changes when working with Boolean objects instead of primitive boolean values. This is what the next section is all about.

2.2. The equals() Method for Boolean Objects

When we’re comparing Boolean objects (Java’s wrapper class for boolean), using == might not yield the results we expect. Boolean is an object, so using == checks for reference equality rather than value equality. To ensure we’re comparing values instead of references, the equals() method is a better choice:

@Test
void givenBooleans_whenUsingEqualsMethodWithBooleanObjects_thenCompare() {
    Boolean a = Boolean.TRUE;
    Boolean b = Boolean.TRUE;
    assertTrue(a.equals(b), 
      "Using equals() on Boolean objects should be true when both values are the same");

    a = Boolean.TRUE;
    b = Boolean.FALSE;
    assertFalse(a.equals(b), 
      "Using equals() on Boolean objects should be false when values differ");
}

In this code, a.equals(b) checks if the underlying boolean values are the same, regardless of whether a and b are distinct Boolean instances.
So, why not use == with Boolean objects?

Let’s consider the following code:

@Test
void givenBooleansTypes_whenUsingDirectOperator_thenCompare() {
    Boolean a = Boolean.TRUE;
    Boolean b = new Boolean(true);
    assertFalse(a == b, 
      "Using == on different boolean objects should be false when values differ");
}

Though a and b both have the value true, using == returns false because a and b are different Boolean instances. Using equals() ensures the comparison is based on the actual value (true or false) rather than the object reference, which is particularly helpful when dealing with nullable values.
We use equals() on Boolean objects to avoid unexpected behavior caused by object reference comparison.

2.3. Using Logical XOR

We should consider using the logical XOR (exclusive OR) operator ^ for a more creative solution. XOR between two booleans returns true if the values differ and false if they’re the same.
By negating the result, we can check if the two booleans are equal:

@Test
void givenBooleans_whenUsingXORApproach_thenCompare() {
    boolean a = true;
    boolean b = true;
    assertTrue(!(a ^ b), 
      "XOR approach should return true when both values are the same");

    a = true;
    b = false;
    assertFalse(!(a ^ b), 
      "XOR approach should return false when values differ");
}

This method works by checking if both values are either true or false. While this approach is less common and slightly harder to read, it’s efficient and useful in scenarios where we might already be using XOR logic.

Using XOR can feel less intuitive, so it’s best suited for situations where XOR logic already makes sense or where performance is critical. If our code’s readability is a priority, the == operator might be a better choice.

2.4. Using Boolean.compare()

Java provides a static method called Boolean.compare() specifically for comparing two boolean values. This method returns 0 if the values are equal, 1 if the first is true and the second is false, and -1 if the first is false and the second is true:

@Test
void givenBooleans_whenUsingBooleanCompare_thenCompare() {
    boolean a = true;
    boolean b = true;
    assertTrue(Boolean.compare(a, b) == 0, 
      "Boolean.compare() should return 0 when both values are the same");

    a = true;
    b = false;
    assertFalse(Boolean.compare(a, b) == 0, 
      "Boolean.compare() should return non-zero when values differ");
}

This approach can be useful when we’re working with sorting or need more than just an equality check. By checking  Boolean.compare(a, b) == 0, we confirm that both booleans are either true or false.

2.5. Avoiding NullPointerException With Objects.equals()

If we’re comparing Boolean objects that might be null, using Objects.equals() from the java.util.Objects package is a good option. It handles null values gracefully, returning true if both are null, or false if only one of them is:

@Test
void givenBooleans_whenUsingObjectsEqualsForNullableBooleans_thenCompare() {
    Boolean a = null;
    Boolean b = null;
    assertTrue(Objects.equals(a, b), 
      "Objects.equals() should return true when both Boolean objects are null");

    a = Boolean.TRUE;
    b = null;
    assertFalse(Objects.equals(a, b), 
      "Objects.equals() should return false when one Boolean is null");

    a = Boolean.TRUE;
    b = Boolean.TRUE;
    assertTrue(Objects.equals(a, b), 
      "Objects.equals() should return true when both Boolean values are the same");

    a = Boolean.TRUE;
    b = Boolean.FALSE;
    assertFalse(Objects.equals(a, b), 
      "Objects.equals() should return false when Boolean values differ");
}

This approach avoids NullPointerException and checks value equality. It’s especially useful when dealing with nullable data or when comparing potentially uninitialized values.

3. Comparison of Approaches

Let’s have a look at a comparison for all the ways discussed.

Method

Use case

Notes

==

Simple, direct comparison of boolean values

Fast and straightforward; avoid with Boolean objects

equals()

Boolean object comparison

Preferred for non-primitive types to avoid reference equality issues

XOR (^)

Creative option, uncommon

Efficient but less readable; use if XOR logic is suitable

Boolean.compare()

Detailed comparison or sorting needs

Returns 0 for equality; useful in sorting

Objects.equals()

Handling potential null values

Safely handles nullable Boolean objects

4. Conclusion

In this article, we saw that checking for the equality between boolean values can be simple yet varied, with each approach offering unique advantages. For basic, direct comparisons, == works best with primitive types, while equals() ensures reliable results with Boolean objects. Methods like XOR, Boolean.compare(), and Objects.equals() add flexibility for specialized needs, such as handling nulls or integrating comparisons in complex logic.

Choosing the right approach helps make our code more readable, safe, and tailored to our specific requirements, ensuring clarity and correctness in boolean comparisons.
As always, the code presented in this article is available over on GitHub.