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.
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.
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:
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.
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 = ;
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.
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.
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.
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: