Initialize All Array Elements to Zero in Java
- Initialize Array Elements to Zero in Java
-
Initialize Array Elements to Zero by Using the
fill()
Method in Java -
Initialize Array Elements to Zero by Using the
nCopies()
Method in Java - Initialize Array Elements to Zero by Reassignment in Java
-
Initialize Array Elements to Zero by Using the
for
Loop in Java

This tutorial introduces programs on how to initialize all array elements to zero in Java. You’ll find useful sample codes to guide you through this process.
In Java, array holds a similar type of data. This object gets initialized at runtime by the compiler; its value depends upon the type of array - this means an integer array will be initialized to 0, and the string array will be null. In this example, we will initialize array elements to zero by using methods like fill()
and nCopies()
, and some custom codes as well.
Initialize Array Elements to Zero in Java
By default in Java, data types like int, short, byte, and long arrays are initialized with 0. So, if you create a new array of these types, you don’t need to initialize them by zero because it’s already their default setting. In the example below, we created two arrays with the int
and byte
types and see their default value is zero.
public class SimpleTesting{
public static void main(String[] args) {
int arr[] = new int[2];
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
byte barr[] = new byte[2];
for (int i = 0; i < barr.length; i++) {
System.out.println(barr[i]);
}
}
}
Output:
0
0
0
0
Initialize Array Elements to Zero by Using the fill()
Method in Java
If an array already has elements and you want to initialize it by 0, you should use the fill()
method of the Arrays
class that fills the specified value to the array. See the example below:
import java.util.Arrays;
public class SimpleTesting{
public static void main(String[] args) {
int arr[] = new int[] {12,23,45,58};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("Array after initialize to zero");
Arrays.fill(arr, 0);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
Output:
12
23
45
58
Array after initialize to zero
0
0
0
0
Initialize Array Elements to Zero by Using the nCopies()
Method in Java
Here, we used the nCopies()
method of the Collections
class that creates a copy of the array, and the toArray()
method converts the elements into an integer array. See the example below:
import java.util.Collections;
public class SimpleTesting{
public static void main(String[] args) {
int arr[] = new int[] {12,23,45,58};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("Array after initialize to zero");
Integer[] arr2 = Collections.nCopies(arr.length, 0).toArray(new Integer[0]);
for (int i = 0; i < arr2.length; i++) {
System.out.println(arr2[i]);
}
}
}
Output:
12
23
45
58
Array after initialize to zero
0
0
0
0
Initialize Array Elements to Zero by Reassignment in Java
This method is a new tricky solution where we create a new array with the same size and type and then assign its reference to the original array that makes the original array reset. As a result, all the elements get initialized to 0. Here’s an example:
public class SimpleTesting{
public static void main(String[] args) {
int arr[] = new int[] {12,23,45,58};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
int[] arr2 = new int[4];
arr = arr2;
System.out.println("Array after initialize to zero");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
Output:
12
23
45
58
Array after initialize to zero
0
0
0
0
Initialize Array Elements to Zero by Using the for
Loop in Java
This process is one of the older and least recommended approaches where we are using a for
loop. In each iteration, we are assigning 0 to each index of the array that makes all the elements of the array initialize to 0. Check this code block as a sample:
public class SimpleTesting{
public static void main(String[] args) {
int arr[] = new int[] {12,23,45,58};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("Array after initialize to zero");
for (int i = 0; i < arr.length; i++) {
arr[i] = 0;
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
Output:
12
23
45
58
Array after initialize to zero
0
0
0
0
Related Article - Java Array
- Concatenate Two Arrays in Java
- Convert Byte Array in Hex String in Java
- Remove Duplicates From Array in Java
- Count Repeated Elements in an Array in Java
- Natural Ordering in Java
- Slice an Array in Java