How to Initialize an array in java?

The different ways in which we can initialize an array are as follows-

Initializing an array at the time of declaration-

We can simply initialize the array by assigning values to it, written inside curly braces(the most common and convenient method) OR we can use the new keyword followed by the data type of the array and rectangular brackets (used for containing array's size).

Keep in mind that the size of your array object will be the number of elements you specify inside the curly brackets. Therefore, in the example, the array is of size three.

//initializing a string array at the time of declaration
String[] numbers = {"zero", "one", "two"};
String[] numbers = new String[] {"zero", "one", "two"};

//initializing an integer array at the time of declaration
int[] numbers = {1, 2, 3};
int[] numbers = new int[]{1, 2, 3};

If you are creating a method that returns an initialized array, you will have to use the new keyword with the curly braces. When returning an array in a method, curly braces alone won't work. Below usage will make it clear to you-

public String[] showColors() {
    //This will Work since we are using the 'new' keyword
    return new String[]{"pink", "blue"}; 

public String[] showColors() {
    // This will not work
    return {"pink", "blue"}; 

initializing an array after its declaration-

In the following code snippet, the String Array of size 3 is declared first. Then in the next line, the individual elements are assigned values. This is a slow way to initialize an array.

String[] numbers = new String[3];
numbers[0] = “zero;
numbers[1] = “one”;
numbers[2] = “two”;

Initializing an array without assigning values-

An array can be initialized to any size without assigning values and in this case, the default value of each element is 0. The elements in the array allocated by 'new' will automatically be initialized to zero (for numeric types), false (for boolean), or null (for reference types), these are the default values.

//intializing the array named as "arr" of size 5
int [] arr=new int[5];
//if we print the elements of the array then we get,
//the default value of each element which is 0
for(int i=0; i<5; i++)

Initializing an array using Arrays.copyOf() -

The Arrays.copyOf() method creates a new array by copying another array. This method accepts the source array and length of the copy array to be created. If the length is greater than the length of the array to be copied, then extra elements will be initialized using their default values. If the source array has not been initialized, then a NullPointerException gets thrown.

int arr[] = { 1, 2, 3 };
//the above array will be copied to the below array named 'copy' by the following method-
int[] copyarr = Arrays.copyOf(arr, 3);

Arrays.setAll() sets all elements of an array using a generator function that accepts an index and generates a corresponding value for that index. If the generator function is null, then a NullPointerException is thrown.

import java.util.Arrays;
public class Example {
  public static void main(String[] args) {
    int size = 5;
    int value = 1;
    int[] arr = new int[size];
    //setting values to array 'arr' using setall() method
    Arrays.setAll(arr, i -> value);


[1, 1, 1, 1, 1]

Initializing an array using Arrays.fill() method-

This method accepts an array and a value and then assigns that value to all the array indexes.

int val = 1;
int[] arr = new int[3];
Arrays.fill(arr, val);
//initialize all elements of the array 'arr' with a specified value "val"


[1, 1, 1]

Initializing an array in java using the Stream API -

You can also use the Java 8 Stream API for making a copy of an Array into another. Example-

String[] array = {"pink", "blue", "green'"};
String[] copiedArray =[]::new);

We can also initialize a multi-dimensional array in the same ways. In java, each row of the multidimensional array can be of different lengths. A sample example is as follows-

public static void main( String[ ] args) {
//create a 2-D array
int[ ][ ] num={ {1,2} , {3, 4, 5} , {6} };
//calculating the length of each row
System.out.println("Length of 1st row is: "+num[0].length);
System.out.println("Length of 2nd row is: "+num[1].length);
System.out.println("Length of 3rd row is: "+num[2].length);


Length of 1st row is: 2
Length of 2nd row is: 3
Length of 3rd row is: 1
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions