boolean array default value java code example

Example 1: default value boolean array java

// The default value of booleans in a boolean array is false

boolean[] boolArray = new boolean[5];

Example 2: how to initalize the array as to false

import java.util.Arrays;
public class BooleanArrayTest {
   public static void main(String[] args) {
      Boolean[] boolArray = new Boolean[5]; // initialize a boolean array
      for(int i = 0; i < boolArray.length; i++) {
         System.out.println(boolArray[i]);
      }
      Arrays.fill(boolArray, Boolean.FALSE);
      // all the values will be false
      for(int i = 0; i < boolArray.length; i++) {
         System.out.println(boolArray[i]);
      }
      Arrays.fill(boolArray, Boolean.TRUE);
      // all the values will be true
      for (int i = 0; i < boolArray.length; i++) {
         System.out.println(boolArray[i]);
      }
   }
}

Tags:

Java Example