Home >Java >javaTutorial >In Java, how do we initialize a boolean array?

In Java, how do we initialize a boolean array?

WBOY
WBOYforward
2023-08-26 08:05:061214browse

In Java, how do we initialize a boolean array?

Boolean array can only be used to store Boolean data type values. The default value of a Boolean array is false. Boolean array is initialized to false, reference type array is initialized to null. In some cases we need to initialize all values ​​of boolean array to true or false. In this case, we can use the Arrays.fill() method.

Syntax

boolean[] booleanArray;

Example

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);
<strong>     </strong> // all the values will be false<strong>
</strong>      for(int i = 0; i < boolArray.length; i++) {
         System.out.println(boolArray[i]);
      }
      Arrays.fill(boolArray, Boolean.TRUE);
<strong>      </strong>// all the values will be true<strong>
</strong>      for (int i = 0; i < boolArray.length; i++) {
         System.out.println(boolArray[i]);
      }
   }
}

Output

null
null
null
null
null
false
false
false
false
false
true
true
true
true
true

The above is the detailed content of In Java, how do we initialize a boolean array?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete