In this post, we’ll see how to declare and initialize arrays in Java.

1. Using new operator

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

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

For example, the following code creates a primitive integer array of size 5 using a new operator and array initializer.

 
Here’s an alternate syntax for declaring an array, where [] appears after the variable name, similar to C/C++ style arrays.

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

2. Splitting declaration and initialization

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

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

 
The first line depicts 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 a single line without using a new operator, as shown below:

Type[] arr = { comma separated values };

 
Here’s the 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 the 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, the following code creates a primitive integer array of size 5. The array will be auto-initialized with a default value of 0.

 
We can use the Arrays.fill() method to assign a specified value to each element of the specified array. For example, the following code creates an array of 5 ints and assigns each element with a 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. We can use it to set all elements of the specified array, using the generator method that computes the desired value for every index.

For example, the following 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 the following syntax:

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

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

6. Using 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 a sequence of increasing values from 1 to 5 by an incremental step of 1. To get a primitive integer array, we can call toArray() method on IntStream, as shown below:

 
We can also use IntStream.range(start, end) that generate a 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 array’s size 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, it grows automatically.
  4. If an array index is either negative or greater than or equal to the array’s size, 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 allows 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 a negative size. For example,

That’s all about declaring and initializing arrays in Java.