Different Ways to Initialize Array in Java

Last updated on February 28th, 2024

An array is an indexed collection of a fixed number of homogeneous data elements in Java. We can represent multiple values with the same name of the same data type so that the readability of the code will be improved. It is fixed in size that is once we create an array there is no chance of increasing or decreasing the size based on our requirements. To use arrays we should know the size in advance which may not be possible.  In this topic, we will learn about different ways to declare and initialize an array in Java.

Declaring an Array in Java

An array is declared by using data type, variable_name with bracket [ ] symbol.
Syntax

data_type [ ] variable_name;
data_type variable_name [ ];

Example

int [ ] a;
int a[ ];

How to initialize an Array in Java?

There are the following ways to initialize an Array in Java:
   • Initialize an Array with default values 
   • Initialize an Array with non-default values 
   • Initialize an Array with the use of Curly braces { }
   • Initialization using Stream Interface

Initialize an Array with default values

When we are creating an array with a new keyword then every element is initialized with default value automatically.

Example

public class SpringJava{
public static void main(String args[]) {
int [ ] a= new int[3];
System.out.println(a);
System.out.println(a[0]);
    }
}

Output

[I@58d25a40
0

→ In the above example whenever we are trying to print any object reference internally toString() method will be executed which is implemented by default to return such as class_name@hexadecimalstringrepresentionofhascode.
→ In the example, we created an array with size 3 and data type integer. For the integer type array default is 0. When creating different data types of arrays, arrays have different default values such as null string is the default value for the String array and false is the default value for the boolean type of array, etc.

Initialize an Array with non-default values 

In this way, we can initial the array with values one by one.
Example

public class SpringJava{
public static void main(String args[]) {
int [] a= new int[3];
a[0]=10;
a[1]=20;
a[2]=30;
System.out.println(a);
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
    }
}

Output

[I@1c655221
10
20
30

Initialize an Array with the use of Curly braces { }

In this way, we can initialize an array in a single line with using curly braces without declaring the size of the array.
Example

public class SpringJava{
public static void main(String args[]) {
int [] a={10,20,30};
System.out.println(a);
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
    }
}

Output

[I@1c655221
10
20
30

Initialization using Stream Interface

We can initialize an array by using the stream interface which generates a stream of values then this is converted into an array. There the some stream interfaces that are used to initialize the integer type of an array:
 • IntStream.range()
 • IntStream.rangeClosed()
 • IntStream.of()

IntStream.range()

This is used to initialize an array of integer types within the specified range. The first parameter of the range is the first element of the array and other elements of the array will be greater than the first parameter and less than the second parameter of the range.

IntStream.rangeClosed()

This is used to initialize an array of integer types within the specified range. The first parameter of the range is the first element of the array and other elements of the array will be greater than the first parameter and less than equal to the second parameter of the range.

IntStream.of()

This is similar to curly braces { } initialization of an array where we can mention each and every element to initialize an array.
Example

import java.util.stream.IntStream;
public class SpringJava{
public static void main(String[] args){
//An array of integer type using IntStream.range() method to initialize
int[ ] arrP = IntStream.range(1, 6).toArray();
for (int i = 0; i < arrP.length; i++) {
System.out.print(arrP[i] + " ");
}
System.out.print('\n');
//An array of integers using IntStream.rangeClosed() method to initialize
int[ ] arrQ = IntStream.rangeClosed(1, 5).toArray();
for (int i = 0; i <arrQ.length; i++) {
System.out.print(arrQ[i] + " ");
}
System.out.print('\n');
//An array of integers using IntStream.of() method to initialize
int[ ] arrR = IntStream.of(1, 2, 3, 4 ,5).toArray();
for (int i = 0; i <arrR.length; i++) {
System.out.print(arrR[i] + " ");
        }
    }
}

Output

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

FAQ

Q1: How can we initialize an array?
Ans. We can use curly braces{} to initialize an array in one single line where we can mention each and every element of an array with comma-separated.
Q2: How to initialize an array to an empty array in Java?
Ans. We can use the new keyword followed by the data type of the elements the array would store.
Q3: How to initialize an array in Java in class?
Ans. We can be initialized by default values when the size of the array is declared with rectangular brackets [ ].

Conclusion

In this topic, we learned about how to initialize an array in Java in different ways.

Leave a Comment