$show=/label

How to create and initialize boolean array in java?

SHARE:

A quick guide to create a boolean array and initialize the boolean array in java and example programs.

1. Overview

In this tutorial, We'll how to use boolean array and how to initialize boolean array in java.

Boolean array is used to store the only boolean values. boolean value can be either true or false.

The default value of boolean type is false and the default value of the primitive boolean array is false for all indexes.

Boolean array references are initialized with null values.

In some cases, the default value is false is not useful. To make the default value true, we need to use the Arrays.fill() method.

How to create and initialize boolean array in java?



2. Creating boolean Array


The boolean array can be created with empty curly braces. That means a boolean array is created with zero values in it.

Secondly, the boolean array is created with crurly braces with values inside it.

The third one is creating the boolean array with a new keyword and the size has to be defined at the time of declaration.

Example
package com.javaprogramto.arrays.booleanarray;

public class BooleanArrayExample {

	public static void main(String[] args) {
		
		boolean[] array1 = {};

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

		boolean[] array3 = new boolean[5];
	}
}



3. Setting Values Into Boolean Primitive And Wrapper Array


Setting the values can be done while declaring the array or after creation of array uisng for loop or any other way.

Example

package com.javaprogramto.arrays.booleanarray;

public class BooleanArrayExample2 {

	public static void main(String[] args) {

		// way 1
		boolean[] array1 = { false, true, true };

		// way 2
		boolean[] array2 = new boolean[4];

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

		// way 3
		boolean[] array3 = new boolean[5];

		for (int i = 0; i < array3.length; i++) {
			array3[i] = i % 2 == 0;
		}

	}
}

4. Accessing Boolean Array Values


We have to use only the index of the boolean array to get the values of it. If you want to get the specific index value then you can psss that index to the array.

We can use the for loop or foreach to iterate the whole boolean array all values.

Look at the below examples.

Example

System.out.println("array1[0] value - " + array1[0]);
System.out.println("array2[1] value - " + array2[1]);
System.out.println("array3 last index value - " + array3[array3.length - 1]);

System.out.println("\nboolean array 3 all values using foreach ");
for(boolean b : array3) {
	System.out.println(b);
}

Output
array1[0] value - false
array2[1] value - false
array3 last index value - true

boolean array 3 all values using foreach 
true
false
true
false
true


5. Verifying Default Value of Boolean Array


Boolean array is initialized with the false as default value. This is the default value gets set to the boolean array in java.

Let us verify the what is the default value of boolean array in java.

Example
package com.javaprogramto.arrays.booleanarray;

public class BooleanArrayExample4 {

	public static void main(String[] args) {

		boolean[] array4 = new boolean[4];

		System.out.println("Checking the default values of boolean array 4 with for each loop");
		for (boolean b : array4) {
			System.out.println(b);
		}

	}
}

Output
Checking the default values of boolean array 4 with for each loop
false
false
false
false


6. Setting Default Values as True in Boolean Array


As we know that default value of boolean array is false. but in some cases, we may need it to be as true rather false.

In this case, we need to use the Arrays.fill() method after delcaring the array with the new keyword.

And also setting to true can be done with the simple for loop.


Example
public class BooleanArrayExample5 {

	public static void main(String[] args) {

		boolean[] array5 = new boolean[5];

		System.out.println("Default value of array5 at index 1 is - " + array5[1]);

		System.out.println("Setting to true for all indexes of array 5 using simple for loop");
		for (int i = 0; i < array5.length; i++) {
			array5[i] = true;
		}
		System.out.println("Now Default value of array5 at index 1 is - " + array5[1]);

	}
}

Output
Default value of array5 at index 1 is - false
Setting to true for all indexes of array 5 using simple for loop
Now Default value of array5 at index 1 is - true

Next, example is on Arrays.fill(array, true)

package com.javaprogramto.arrays.booleanarray;

import java.util.Arrays;

public class BooleanArrayExample6 {

	public static void main(String[] args) {

		boolean[] array6 = new boolean[5];

		System.out.println("Default values of array5 are " + Arrays.toString(array6));

		Arrays.fill(array6, true);

		System.out.println("New Default values of array5 are " + Arrays.toString(array6));
	}
}

Output
Default values of array5 are [false, false, false, false, false]
New Default values of array5 are [true, true, true, true, true]


7. Boolean Array Example


A simple program to create the boolean array and assing the values to it in single example.

import java.util.Arrays;

public class BooleanArrayExample7 {

	public static void main(String[] args) {

		boolean[] array1 = {};
		System.out.println("array1 length - " + array1.length);

		boolean[] array2 = new boolean[2];
		System.out.println("array2 values - " + Arrays.toString(array2));

		for (int i = 0; i < array2.length; i++) {
			array2[i] = true;
		}

		System.out.println("New array2 values - " + Arrays.toString(array2));

		System.out.println("Acccessing array values by index - " + array2[1]);

		boolean[] array7 = new boolean[5];

		System.out.println("Default values of array7 are " + Arrays.toString(array7));

		Arrays.fill(array7, true);

		System.out.println("New Default values of array7 are " + Arrays.toString(array7));
	}
}

Output
array1 length - 0
array2 values - [false, false]
New array2 values - [true, true]
Acccessing array values by index - true
Default values of array7 are [false, false, false, false, false]
New Default values of array7 are [true, true, true, true, true]

8. Conclusion


In this article, we've seen all cocepts of boolean array in java with examples.




COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: How to create and initialize boolean array in java?
How to create and initialize boolean array in java?
A quick guide to create a boolean array and initialize the boolean array in java and example programs.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEif68PMX-OIRP85WYboXKXWAVz9v3bpyq-OX0ZY8YWcMjQsE2VwlmT3Nb1EuQay92PNopfYk_9jHsMicOXlHhGFp7qdc3wQI8xj3BrQqMnYWOd1dBgBAZ5Ig3mZ4qwdyIGjg1I9TzSkP60/w640-h418/How+to+create+and+initialize+boolean+array+in+java%253F.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEif68PMX-OIRP85WYboXKXWAVz9v3bpyq-OX0ZY8YWcMjQsE2VwlmT3Nb1EuQay92PNopfYk_9jHsMicOXlHhGFp7qdc3wQI8xj3BrQqMnYWOd1dBgBAZ5Ig3mZ4qwdyIGjg1I9TzSkP60/s72-w640-c-h418/How+to+create+and+initialize+boolean+array+in+java%253F.png
JavaProgramTo.com
https://www.javaprogramto.com/2021/11/java-boolean-array.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2021/11/java-boolean-array.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content