How to Initialize Java Boolean Array

In this tutorial we will discuss about java boolean array . We will see how to create and initialize boolean array in java , what is default value of array of booleans , print boolean array and assign value to boolean array using Arays.fill() method .

Java boolean Array

Firstly we discuss how to create boolean array in java . We can create array of booleans in java using these three methods . In method 1 we create a empty  java boolean array and in method 2 we create and initialize with value and in method3 create array of booleans with keyword with default values .

public class CreateBooleanArray {
          public static void main(String[] args) {

        	  //method1
	           boolean[] arr1 = {};
              
	           
	           //method2
				boolean[] arr2 = { true, false, true };

				//method3
				boolean[] arr3 = new boolean[5];
			}
		}

Creating boolean array java with default value

Now we will create boolean and Boolean array in java using new keyword . Default value of boolean array java is false for each element and Default value of Boolean array is null for each element .

Note : The default value of the primitive boolean array is false and whereas the Boolean object array is null.

public class BooleanArray {
       public static void main(String[] args) {
    	   boolean[] booleanArray = new boolean[5];
    	   Boolean[] BooleanArray = new Boolean[5];
    	   System.out.println("printing default value of boolean array");
    	   
    	   for (boolean b : booleanArray) {
			System.out.println("default value of boolean array : "+b);
		  }
     	   System.out.println("printing default value of Boolean array");
    	   for (Boolean b : BooleanArray) {
    		   System.out.println("default value of Boolean array : "+b);
		}
	}
}

Output:

printing default value of boolean array
default value of boolean array : false
default value of boolean array : false
default value of boolean array : false
default value of boolean array : false
default value of boolean array : false
printing default value of Boolean array
default value of Boolean array : null
default value of Boolean array : null
default value of Boolean array : null
default value of Boolean array : null
default value of Boolean array : null

Initialize  Java boolean Array

We can initialize boolean array in java by setting value on indexes or using Array.fill() method . Now we discuss how to set value of index element in array of booleans .

We can initialize java boolean array using curly bracket as method 1 . In method 2 we create boolean array java using new keyword and update value of element using indexes . Method 3 we update element of array of booleans using loop . We can print  boolean array using loop .

public class BooleanArray1 {
	
	public static void main(String[] args) {
			
		//method 1
		boolean[] arr1 = { false, true, true };

		System.out.println("print boolean array arr1");
		//print values of boolean arrays
		for (boolean b : arr1) {
			System.out.println("value of arr1 :"+ b);
		}
		
		//method 2
		boolean[] arr2 = new boolean[4];

		arr2[0] = true;
		arr2[1] = false;
		arr2[2] = true;
		arr2[3] = false;

		System.out.println("print boolean array arr2");
		//print values of boolean arrays
		for (boolean b : arr2) {
			System.out.println("value of arr2 :"+ b);			
		}
		
		//method 3
		boolean[] arr3 = new boolean[5];

		for (int i = 0; i < arr3.length; i++) {
			arr3[i] = i % 2 == 0;
		     }
	
		System.out.println("print boolean array arr3");
	
		//print values of boolean arrays
		for (boolean b : arr3) {
			System.out.println("value of arr3 :"+ b);
		}
		
	
	}

}

Output:

print boolean array arr1
value of arr1 :false
value of arr1 :true
value of arr1 :true
print boolean array arr2
value of arr2 :true
value of arr2 :false
value of arr2 :true
value of arr2 :false
print boolean array arr3
value of arr3 :true
value of arr3 :false
value of arr3 :true
value of arr3 :false
value of arr3 :true

If we need to initialize all values of the boolean array with true or false , then we can use Arrays.fill() method . As we have discussed default value of Boolean array is null . In this example first we assign false to each element of Boolean array(booleanArray) and print boolean array element , then we assign true to each element of Boolean array(booleanArray) and print array element .

import java.util.Arrays;

public class BooleanArray2 {
      public static void main(String[] args) {
      	   Boolean[] booleanArray = new Boolean[5];
    	   System.out.println("printing default value of Boolean array");
    	   
    	   for (int i=0; i< booleanArray.length ;i++) {
   		System.out.println("default value of Boolean array : "+ booleanArray[i]);
   		  }
    	  
    	   //Assign the false value to each element of array 
    	   Arrays.fill(booleanArray, false);
   	    
    	   System.out.println("printing false value of Boolean array");
    	  for (boolean b : booleanArray) {
  			System.out.println("Value of Boolean array : "+b);
  		  }    	  

   	     //Assign the true value to each element of array
    	  Arrays.fill(booleanArray, true);
    	  System.out.println("printing true value of Boolean array");
    	  for (boolean b : booleanArray) {
  			System.out.println("Value of Boolean array : "+b);
  		  }    	  
    	   
    	   
	}
}

Output:

printing default value of Boolean array
default value of Boolean array : null
default value of Boolean array : null
default value of Boolean array : null
default value of Boolean array : null
default value of Boolean array : null
printing false value of Boolean array
Value of Boolean array : false
Value of Boolean array : false
Value of Boolean array : false
Value of Boolean array : false
Value of Boolean array : false
printing true value of Boolean array
Value of Boolean array : true
Value of Boolean array : true
Value of Boolean array : true
Value of Boolean array : true
Value of Boolean array : true

In this tutorial we have learned how to initialize java boolean array and how to print boolean array java. You can learn more java program for practice .

Leave a Reply

Your email address will not be published. Required fields are marked *

13 + = 17