QR Code for this Page
Java boolean Array

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

What does boolean mean in Java ?

boolean is a primitive data type in Java. boolean means true or false i.e. logical true or false.

How to declare 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.

boolean[] booleanArray;

Initializing a boolean Array in Java

The boolean array will be initialized to false when you allocate it. All arrays in Java are initialized to the default value for the type. This means that arrays of ints are initialised to 0, arrays of booleans are initialised to false and arrays of reference types are initialised to null.

boolean[] booleanArray = new boolean[10];

What is the Initial or Default Value of a boolean Array in Java ?

The initial value of a boolean array is false. Because the default value of a boolean data type in Java is false.

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

How to fill boolean Array with all true in Java ?

With the Arrays.fill method you can fill boolean array with all true. The java.util.Arrays.fill(boolean[] a, boolean val) method assigns the specified boolean value to each element of the specified array of booleans. Below example shows how to use Arrays.fill method.

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

How to fill boolean Array with all false in Java ?

With the Arrays.fill method you can fill boolean array with all false. The java.util.Arrays.fill(boolean[] a, boolean val) method assigns the specified boolean value to each element of the specified array of booleans. Below example shows how to use Arrays.fill method.

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

How to Convert boolean Array to int array in Java ?

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

How to Convert boolean Array to String in Java ?

The java.util.Arrays.toString(boolean[]) method returns a string representation of the contents of the specified boolean array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space).

The following example shows the usage of java.util.Arrays.toString() method.

How to Convert Java boolean array to String Example

/* How to Convert Java boolean array to String Example */
/* Save with file name ConvertBooleanArrayToString.java */

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

		System.out.println("converted boolean array to String");
		System.out.println(Arrays.toString(boolean_array);

	}
}