Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

Boolean is a fundamental data type in Java. Usually, it can have only two values, true or false.

In this tutorial, we'll discuss how to toggle a given boolean variable.

2. Introduction to the Problem

This problem is pretty straightforward. Simply put, we want to invert the value of a boolean variable. For example, true becomes false after toggling.

However, we should note that there are two “different” boolean types in Java, the primitive boolean and the boxed Boolean. Therefore, the ideal toggle method should work for both types.

In this tutorial, we'll address how to implement such a method.

Also, for simplicity, we'll use unit test assertions to verify if our implementations work as expected.

So next, let's start with toggling a primitive boolean variable, as this is the base of our final toggle() method.

3. Toggling a Primitive boolean Variable

The most straightforward way to toggle a primitive boolean variable would be using the NOT operator(!).

Let's create a test to see how it works:

boolean b = true;
b = !b;
assertFalse(b);

b = !b;
assertTrue(b);

If we run this test, it passes. Therefore, every time we perform the NOT operator on a boolean variable, its value will be inverted.

Alternatively, the XOR operator (^) can also invert a boolean. Before considering the implementation, let's quickly understand how the XOR operator works.

Given two boolean variables b1 and b2, b1 ^ b2 is true only if b1 and b2 have different values, for example:

  • true ^ true = false
  • false ^ false = false
  • true ^ false = true

Therefore, we can make use of XOR's characteristics, performing b ^ true to invert b‘s value:

  • b = true -> b ^ true  becomes true ^ true = false
  • b = false -> b ^ true becomes false ^ true = true

Now that we understand the XOR's logic, translating it into Java code isn't a challenging task for us:

boolean b = true;
b ^= true;
assertFalse(b);

b ^= true;
assertTrue(b);

Unsurprisingly, the test passes when we give it a run.

4. Creating the toggle() Method

We've seen that a primitive boolean variable can only have two values: true and false. However, unlike the primitive boolean, the boxed Boolean variable can hold null.

Java automatically unboxes Boolean to boolean when we perform NOT or XOR operation on a Boolean variable. But if we don't handle the null case properly, we'll encounter NullPointerException:

assertThatThrownBy(() -> {
    Boolean b = null;
    b = !b;
}).isInstanceOf(NullPointerException.class);

If we execute the test above, it passes. Unfortunately, this means that NullPointerException occurs when we perform !b.

So next, let's create the null-safe toggle() method to work with both Boolean and boolean variables:

static Boolean toggle(Boolean b) {
    if (b == null){
        return b;
    }
    return !b;
}

Here, we first perform a nullcheck and then use the NOT operator to invert the value. Of course, if we like, after the null-check, we could also use the XOR approach to invert b‘s value.

Finally, let's create a test to verify if our toggle() method works for all cases:

// boxed Boolean
Boolean b = true;
b = ToggleBoolean.toggle(b);
assertFalse(b);

b = ToggleBoolean.toggle(b);
assertTrue(b);

b = null;
b = ToggleBoolean.toggle(b);
assertNull(b);

// primitive boolean
boolean bb = true;
bb = ToggleBoolean.toggle(bb);
assertFalse(bb);
bb = ToggleBoolean.toggle(bb);
assertTrue(bb);

As the test above shows, we tested the toggle() method with a Boolean variable and a boolean variable. Further, we've tested the scenario that the Boolean variable b=null.

The test passes when we execute it. Therefore, our toggle() method works as expected.

5. Conclusion

In this article, we've learned how to build a null-safe method to toggle a given boolean/Boolean variable.

As always, the code relating to this article is available over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!