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

In Java, how do we initialize a boolean array?

WBOY
Release: 2023-08-26 08:05:06
forward
1248 people have browsed it

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;
Copy after login

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]);
      }
   }
}
Copy after login

Output

null
null
null
null
null
false
false
false
false
false
true
true
true
true
true
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template