Declare and Initialize Arrays in Java

In this post, we will illustrate how to declare and initialize arrays in Java.


 

1. Using new operator

We can declare and initialize arrays in Java by using new operator with array initializer. Here’s the syntax –

Type[] arr = new Type[] { comma separated values };

For example, below code creates an integer array of size 5 using new operator and array initializer.

Here’s alternate syntax for declaring an array where [] appears after the variable name, similar to C/C++ style arrays. This can be used in every example in this post.

Type arr[] = new Type[] { comma separated values };

 

2. Splitting declaration and initialization

Here’s is how we can split declaration and initialization of the array.

Type[] arr;
arr = new Type[] { comma separated values };

The first line depict the array declaration and the second line represents the array initialization. For example,

 

3. Shortcut syntax with array initializer

We can also declare and initialize arrays in single line without using new operator as shown below:

Type[] arr = { comma separated values };

Here’s code:

 

4. Using default values in initialization of array

If we don’t provide any initializer, the default value of 0 is assigned to each element in case of short or int or long or byte array. For double or float, the default value is 0.0 and the default value is null for String.

Type[] arr = new Type[capacity];

For example, below code creates an integer array of size 5. The array will be auto-initialized with default value of 0.

 
We can use Arrays.fill() method to assign specified value to each element of the specified array. For example, below code creates an array of 5 ints and assign each element with value of 1.

 
Arrays.fill() also has an overloaded version that can be used to assign a value to each element of the specified range of the array.

 
Java 8 introduced several enhancements to the Arrays class, like introduction of Arrays.setAll() method. It can be used to set all elements of the specified array, using the generator function that computes the desired value for every index.

For example, below code creates an array of 5 ints and sets all of its elements to their respective index value:

 

5. Using Reflection

We can declare and initialize arrays with reflection, using below syntax:

Type[] arr = (Type[]) Array.newInstance(Type.class, capacity);

It creates a new array with the specified type and length with default value of 0. Here’s how we can create an array of integers of length 5:

 

6. Java 8 – IntStream

In java 8, we can use IntStream.rangeClosed() to generate a sequential ordered IntStream between two specified indexes.

For example, IntStream.rangeClosed(1, 5) will generate sequence of increasing values from 1 to 5 by an incremental step of 1. To get an get an array of ints, we can call toArray() method on IntStream as shown below:

 
We can also use IntStream.range(start, end) that generate sequence of increasing values from start to end (exclusive).

 

Few Points to remember –

 

1. int[] a, b; is not the same as int a[], b;

2. The size of an array is fixed and cannot be modified after the array is created. The length field in an array is final and stores the size of an array.

3. If we need a resizable-array implementation, we should use an ArrayList. As elements are added to an ArrayList, its grows automatically.

4. If an array index is either negative or greater than or equal to the size of the array, an ArrayIndexOutOfBoundsException is thrown to indicate that an array has been accessed with an illegal index. To avoid it, we can check that the index is within the limits of the array.

5. Java also allow to have arrays of size 0 as shown below:

or

Since it’s an empty array, it throws an ArrayIndexOutOfBoundsException if we try to read elements from it or assign elements to it.

6. A NegativeArraySizeException will be thrown if a program tries to create an array with negative size. For example,

 
 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz