How can we initialize a boolean array in Java?

Initializing a Boolean Array in Java refers to the process of assigning values (allocating memory) to the Boolean array for the first time. We can initialize an array or any variable -

  • Either at the time of creation.
  • Or, later in the program, using the assignment operator when we are just defining it initially.

A Boolean array can be used to store only Boolean values (i.e., either true or false), and the "default value" of each element in a Boolean array is false. 

In some cases, we may need to initialize all values of a Boolean array to true or false. This can be done using the built-in Arrays.fill() method, by assigning values using square brackets [], or by directly specifying the values inside curly braces {}.

Initializing a Boolean Array at Creation

This is one of the basic ways to initialize a Boolean array in Java by specifying values inside curly braces {}. The following is the syntax to initialize a Boolean array using curly braces -

boolean[] booleanArray = {false, true, true, false, true};

Example

In the following example, we initialize a Boolean Array by specifying the Boolean values inside curly braces {} -

public class booleanArray {
   public static void main(String[] args) {
      //initialize a boolean array by specifying values inside curly braces
      boolean booleanArray[] = {false, false, true, true, false};
      
      System.out.println("Boolean array has been initialized using curly braces.");
      
      //printing boolean array values
      System.out.print("The Boolean array values are: ");
      for(int i = 0; i<booleanArray.length; i++){
          System.out.print(booleanArray[i] + " ");
      }
   }
}

Following is the output of the above program -

Boolean array has been initialized using curly braces.
The Boolean array values are: false false true true false

Initializing Using the Assignment Operator

This is another way to initialize a Boolean Array in Java with a fixed size, where all elements default to false. The following is the syntax to initialize a Boolean array in Java with a fixed size -

boolean[] booleanArray = new boolean[size];

Example

In the example below, we initialize a Boolean Array by specifying a fixed size of 5 and assign Boolean values to it using square brackets '[]' -

public class booleanArray {
   public static void main(String[] args) {
      //initialize a Boolean array with fixed size 5
      boolean booleanArray[] = new boolean[5];
      
      System.out.println("Boolean array has been initialized with fixed size 5.");
      
      //assigning values
      booleanArray[0] = true;
      booleanArray[1] = true;
      booleanArray[2] = false;
      booleanArray[3] = true;
      booleanArray[4] = false;
      
      //printing Boolean array values
      System.out.print("The Boolean array values are: ");
      for(int i = 0; i<booleanArray.length; i++){
         System.out.print(booleanArray[i] + " ");
      }
   }
}

The above program produces the following output -

Boolean array has been initialized with fixed size 5.
The Boolean array values are: true true false true false 
Updated on: 2025-05-05T13:46:37+05:30

25K+ Views

Advertisements