Course – LS – All

Get started with Spring and Spring Boot, 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 initialize an array of boolean values.

2. Introduction to the Problem

The problem’s pretty straightforward. Simply put, we want to initialize an array of boolean variables with the same default value.

However, Java has two “different” boolean types, the primitive boolean and the boxed Boolean. Therefore, in this tutorial, we’ll cover both cases and address how to initialize an array of boolean and Boolean.

Also, for simplicity, we’ll use unit test assertions to verify if our array initializations work as expected.

So next, let’s start with the primitive boolean type.

3. Initializing a Primitive boolean Array

In Java, a primitive variable has a default value. For example, a primitive int variable’s default value is 0, and a primitive boolean variable will hold false by default.

Therefore, if we want to initialize a boolean array with all false, we can simply create the array without setting the values.

Next, let’s create a test to verify it:

boolean[] expected = { false, false, false, false, false };
boolean[] myArray = new boolean[5];
assertArrayEquals(expected, myArray);

If we run the test above, it passes. As we can see, boolean[] myArray = new boolean[5]; initializes five false elements in the boolean array.

It’s worth mentioning that when we want to compare two arrays based on their values, we should use the assertArrayEquals() method instead of assertEquals(). This is because the array’s equals() method checks whether the references of two arrays are the same.

So, initializing an array of primitive false is easy, as we can make use of the boolean‘s default value. However, sometimes, we may want to create an array of true values. If this is the case, we must somehow set the values to true. Of course, we can set them individually or within a loop. But the Arrays.fill() method makes the work easier:

boolean[] expected = { true, true, true, true, true };
boolean[] myArray = new boolean[5];
Arrays.fill(myArray, true);
assertArrayEquals(expected, myArray);

Again, the test passes if we give it a run. So, we’ve got an array of true.

4. Initializing a Boxed Boolean Array

So far, we’ve learned how to initialize an array of primitive boolean with true or false. Now, let’s look at the same operations on the boxed Boolean side.

First, unlike a primitive boolean variable, a boxed Boolean variable doesn’t have a default value. if we don’t set a value to it, its value is null. Therefore, if we create an array of Boolean without setting elements’ values, all elements are nulls. Let’s create a test to verify it:

Boolean[] expectedAllNull = { null, null, null, null, null };
Boolean[] myNullArray = new Boolean[5];
assertArrayEquals(expectedAllNull, myNullArray);

If we want to initialize the Boolean array with true or false, we can still use the Arrays.fill() method:

Boolean[] expectedAllFalse = { false, false, false, false, false };
Boolean[] myFalseArray = new Boolean[5];
Arrays.fill(myFalseArray, false);
assertArrayEquals(expectedAllFalse, myFalseArray);
                                                                   
Boolean[] expectedAllTrue = { true, true, true, true, true };
Boolean[] myTrueArray = new Boolean[5];
Arrays.fill(myTrueArray, true);
assertArrayEquals(expectedAllTrue, myTrueArray);

The test above passes when we run it.

5. Conclusion

In this short article, we’ve learned how to initialize a boolean or Boolean array in Java.

As always, the full source code for the examples is available over on GitHub.

Course – LS – All

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

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.