Open In App

How to Initialize an Array in Java?

Last Updated : 14 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

An array is a data structure in Java that is used to store data objects having the same data type. Each and every element in an array has a unique index value. In an array, we have to declare its size first and the size of the array is fixed. In an array, we can store elements of different data types like integer, string, date and etc. In this article, we will discuss about different ways to declare and initialize an array.

Declaring an Array in Java

An array is declared by using its data type and identifier. In Java, arrays are declared in a similar way as other variables are declared but an extra bracket [] is added when we declare an array.

Syntax:

int arr [];
int [] arr;

Here, size of the array is not mentioned because a reference of an array is created in the memory. It can be also known as a memory address of an array.

How to Initialize an Array in Java?

After declaring an array we have to initialize it with values as we have to do it with other variables. In an array, we have to assign multiple values, so the initializing process is not as simple as variables. We can initialize an array with default or non-default values. Below are different methods to initialize an Array:

  • Initialize an Array with default values 
  • Initialize an Array with non-default values 
  • Initialize an Array using Curly braces { }
  • Initialization using stream Interface

Initialize an Array with default values 

In Java, an array can be initialized by default values when the size of the array is declared with rectangular brackets [ ].

int [] arr = new int[20];

 In the above code, an array of size 20 is declared where the data type is integer. Different data types have different default values which are initialized at the time of declaration. For integer type array default value is 0, false is the default value for boolean type array and empty string is the default value for string type array.

Initialize an Array with non-default values 

In Java, we can also initialize an array with particular values. For that, we need to initialize each value one by one. But this method is only useful for small sizes of arrays not for arrays having large sizes. For large-size arrays, we have to use a loop to initialize non-default values.

In the following example, an integer type array of size 6 is declared and then 6 non-default values are initialized in it.

int[] arr = new int[6];
arr[0] = 8;
arr[1] = 6;
arr[2] = 4;
arr[3] = 2;
arr[4] = 1;
arr[5] = 9;

Initialize an Array using Curly braces { }

An array can also be initialized by using curly braces where we don’t have to declare the size of the array. All the non-default values are initialized in the curly braces which are separated by a comma.

In the following example, a string-type array is initialized with non-default values using curly braces.

String[] arrStr = {"Geeks", "of", "Geeks"};

Initialization using stream Interface

An array can be initialized by using a stream interface which generates a stream of values and then it is converted into an array. Below are three instream interfaces that are used to initialize an integer type array.

  • IntStream.range()
  • IntStream.rangeClosed()
  • IntStream.of()

1. IntStream.range()

It is used to initialize an array of integers within a given range. The first parameter in the range will be the first element of the array and the other elements in the array will be greater than that element but less than the second parameter of the range.

2. IntStream.rangeClosed()

We can also use rangeClosed() instead of range() if we want to add the last element of the range in an array. The first parameter in the rangeClosed() method will be the first element of the array and the other elements in the array will be greater than that element but less than and equal to the second parameter of the rangeClosed() method.

3. IntStream.of()

This method works similarly to curly braces where we have to mention each and every element individually that we want to assign to the array. In the following code, we have assigned values to the array by using these three interfaces.

Example:

Java




// Java program to demonstrate different ways of
// initializing an integer array.
import java.util.stream.IntStream;
  
// Driver Clas
public class Main {
    // Main function
    public static void main(String[] args)
    {
  
        // an array of integers using IntStream.range()
        // method
        int[] arr1 = IntStream.range(1, 5).toArray();
        for (int i = 0; i < arr1.length; i++) {
            System.out.print(arr1[i] + " ");
        }
  
        System.out.print('\n');
  
        // an array of integers using
        // IntStream.rangeClosed() method
        int[] arr2 = IntStream.rangeClosed(1, 4).toArray();
  
        for (int i = 0; i < arr2.length; i++) {
            System.out.print(arr2[i] + " ");
        }
  
        System.out.print('\n');
  
        //  an array of integers using IntStream.of()
        // method
        int[] arr3 = IntStream.of(1, 2, 3, 4).toArray();
        for (int i = 0; i < arr3.length; i++) {
            System.out.print(arr3[i] + " ");
        }
    }
}


Output

1 2 3 4 
1 2 3 4 
1 2 3 4 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads