Java boolean Array

boolean Array in Java

Java boolean array is used to store boolean data type values only. The default value of the elements in a Java boolean array is false.

With the following Java boolean array examples you can learn

  • how to declare Java boolean array
  • how to assign values to Java boolean array
  • how to get values from Java boolean array

Initializing a boolean Array in Java

Arrays are declared with [] (square brackets). If you put [] (square brackets) after any variable of any type only that variable is of type array remaining variables in that declaration are not array variables those are normal variables of that type.

If you put [] (square brackets) after any data type all the variables in that declaration are array variables. All the elements in the array are accessed with index. The array element index is starting from 0 to n-1 number i.e. if the array has 5 elements then starting index is 0 and ending index is 4.

// declares an array of booleans
boolean[] booleanArray;

What is the Length of an Array in Java ?

In Java all the arrays are indexed and declared by int only. That is the size of an array must be specified by an int value and not long or short. All the arrays index beginning from 0 to ends at 2147483646. You can store elements upto 2147483647. If you try to store long (big) elements in array, you will get performance problems. If you overcome performance problems you should go to java collection framework or simply use Vector.

Syntax :
                        < data type > < variable >[];
                        < data type >[] < variable >
			    

Java boolean Array Example

/* Java boolean Array Example */
/* Save with file name BooleanArray.java */

public class BooleanArray
{
	public static void main(String args[])
	{
		//BOOLEAN ARRAY DECLARATION

		boolean array[];

		//MEMORY ALLOCATION FOR BOOLEAN ARRAY

		array = new boolean[4];

		//ASSIGNING ELEMENTS TO BOOLEAN ARRAY

		array[0] = true;

		array[1] = false;

		array[2] = false;

		array[3] = false;

		//BOOLEAN ARRAY OUTPUT

		System.out.println("Java boolean Array Example");

		for(int i=0;<array.length;i++)
		{
			System.out.println("boolean array Element at : "+ i + " " + array[i]);
		}
	}
}
				    

In this example you can learn how to assign values to Java boolean array at the time of declaration.

How to assign values to Java boolean array at the time of declaration

/* How to assign values to Java boolean array at the time of declaration Example */
/* Save with file name BooleanArray2.java */

public class BooleanArray2
{
	public static void main(String args[])
	{
		//BOOLEAN ARRAY DECLARATION AND ASSIGNMENT

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

		//BOOLEAN ARRAY OUTPUT

		System.out.println("Java boolean Array Example");

		for(int i=0;i<array.length;i++)
		{
			System.out.println("boolean array Element at : "+ i + " " + array[i]);
		}
	}
}
				    

In this example you can learn how to declare Java boolean array with other boolean variables.

How to declare Java boolean array with other boolean variables

/* How to declare Java boolean array with other boolean variables Example */
/* Save with file name BooleanArray3.java */

public class BooleanArray3
{
	public static void main(String args[])
	{
		//BOOLEAN ARRAY DECLARATION WITH OTHER NORMAL VARIABLES

		boolean array1[], a; //array1 IS AN ARRAY a IS NOT AN ARRAY

		//ASSIGN VALUES TO BOOLEAN ARRAY

		array1 = new boolean[5];

		array1[0] = true;

		array1[1] = false;

		array1[2] = false;

		array1[3] = false;

		a = true;

		//BOOLEAN ARRAY OUTPUT

		System.out.println("Java boolean Array Example");

		System.out.println("a value is : "+a);

		for(int i=0;i<array1.length;i++)
		{
			System.out.println("boolean array1 Element at : "+ i + " " + array1[i]);
		}
	}
}
				    

In this example you can learn how to assign Java boolean array to other Java boolean array.

How to assign Java boolean array to other Java boolean array

/* How to assign Java boolean array to other Java boolean array Example */
/* Save with file name BooleanArray4.java */

public class BooleanArray4
{
	public static void main(String args[])
	{
		//BOOLEAN ARRAYS DECLARATION

		boolean[] array1, array2; //array1 AND array2 ARE ARRAY VARIABLES

		//ASSIGN VALUES TO BOOLEAN ARRAY

		array2 = new boolean[5];

		array2[0] = true;

		array2[1] = false;

		array2[2] = false;

		array2[3] = false;

		//ASSIGNING array2 ARRAY TO array1 ARRAY VARIABLE

		array1 = array2;

		//BOOLEAN ARRAY OUTPUT

		System.out.println("Java boolean Array Example");

		System.out.println("array2 array values");

		for(int i=0;i<array2.length;i++)
		{
			System.out.println("boolean array2 Element at : "+ i + " " + array2[i]);
		}

		System.out.println("array1 array values");

		for(int i=0;i<array1.length;i++)
		{
			System.out.println("boolean array1 Element at : "+ i + " " + array1[i]);
		}
	}
}
				    

In the following example you can learn how to assign all the elements of Java boolean array to true or fill the Java boolean array with true or how to set Java boolean Array all true.

Assign all the elements of Java boolean array to true Example

/* Assign all the elements of Java boolean array to true Example*/
/* Save with file name FillBooleanArrayExampleTrue.java */

import java.util.Arrays;

public class FillBooleanArrayExampleTrue
{
	public static void main(String args[])
	{
		//BOOLEAN ARRAYS DECLARATION

		boolean[] seats = new boolean[10];

		//Assign all the elements of boolean array to true

		Arrays.fill(seats, true);

		System.out.println("seats array values");

		//BOOLEAN ARRAY OUTPUT

		for(int i=0;i<seats.length;i++)
		{
			System.out.println("boolean array Element at : "+ i + " " + seats[i]);
		}
	}
}
				    

In the following example you can learn how to assign all the elements of Java boolean array to false or fill Java boolean array with false.

Assign all the elements of Java boolean array to false Example

/* Assign all the elements of Java boolean array to false Example*/
/* Save with file name FillBooleanArrayExampleFalse.java */

import java.util.Arrays;

public class FillBooleanArrayExampleFalse
{
	public static void main(String args[])
	{
		//BOOLEAN ARRAYS DECLARATION

		boolean[] seats = new boolean[10];

		//Assign all the elements of boolean array to false

		Arrays.fill(seats, false);

		System.out.println("seats array values");

		//BOOLEAN ARRAY OUTPUT

		for(int i=0;i<seats.length;i++)
		{
			System.out.println("boolean array Element at : "+ i + " " + seats[i]);
		}
	}
}
				    

In the following example you can learn how to convert Java boolean array into Java int array.

How to Convert Java boolean array into Java int array Example

/* How to Convert Java boolean array into Java int array Example */
/* Save with file name ConvertBooleanArrayToIntArray.java */

public class ConvertBooleanArrayToIntArray
{
	public static void main(String args[])
	{
		//BOOLEAN ARRAYS DECLARATION
		boolean boolean_array[] = {true, false, true, true};

		//CREATE INT ARRAY
		int boolean_to_int[] = new int[boolean_array.length];

		//CONVERT boolean to int
		for(int i=0;i< boolean_array.length;i++)
		{
		    //CHECK IF ELEMENT IS TRUE
		    if(boolean_array[i])
		    {
			    boolean_to_int[i] = 1;
		    }
		    else
		    {
			    boolean_to_int[i] = 0;
		    }
		}

		System.out.println("converted boolean to int array values");

		//int ARRAY OUTPUT

		for(int i=0;i<boolean_to_int.length;i++)
		{
			System.out.println("int array Element at : "+ i + " " + boolean_to_int[i]);
		}
	}
}