How to Initialize an Array in Java?

Last Updated : 7 Jan 2026

In Java, an array is a data structure that allows us to store multiple values of the same type in a single variable. It provides a convenient way to work with collections of data. Before initialize an array, its declaration is must.

To read more Java Arrays

Array Declaration

Here, the datatype is the type of element that we want to store in the array, square bracket[] is for the size of the array, and arrayName is the name of the array.

Array Initialization

In order to store values in the array, it is required to initialize it after declaration.

In Java, there is more than one way of initializing an array which is as follows:

Instantiation

Declaration and Instantiation

The array is constructed with a certain length with this type of declaration, and the components are initialised with default values (such as 0 for numeric types and null for reference types).

Declaration and Initialization of an Array with Values

In this type of declaration, the array is created and initialized with specific values in single statement.

Declaration and Initialization of a Multidimensional Array

A multidimensional array having several dimensions, such as a 2D or 3D array, is created by this kind of statement. The array's elements can each hold multiple values.

Declaration of an Empty Array

With this kind of declaration, a zero-sized array is created. It may come in handy when you want an empty array with no elements, depending on the situation.

Example: Without Assigning Values

In the following program, we have taken an integer array of length 5. It means that we can store 5 elements in the array. Note that we have not assigned any value in the array. While we print the given array to the console, it prints the default values to the console i.e. 0.

Example

Compile and Run

Output:

0
0
0
0
0

Example: Initialize an Array After the Declaration

In the following program, first we have declared an array (numbers[]) of type int. After that, we have used the new keyword to creates a new integer array with the specified values. While we print the given array to the console, it prints the specified values to the console i.e. 0.

Example

Compile and Run

Output:

22
33
44
55
66

Example: Declaration, Initialization, and Assigning Values in Single Statement

In the following program, we have declared, initialize an array and also assigned values in a single statement.

Example

Compile and Run

Output:

22
33
44
55
66

Note that we can write the following statement in short as follows:

Both statements create an integer array in Java, but there are subtle differences in syntax, usage, and flexibility.

Short Syntax: int[] numbers = {………..};

  • Shortcut Syntax: It is a shorthand for array initialization.
  • Allowed only at Declaration: We can use this form only when declaring the array variable.
  • Cleaner and Simpler: No need to explicitly write.

Full Syntax (Explicit): int[] numbers = new int[]{……….};

  • Full Form Syntax: It explicitly creates a new array object.
  • More Flexible: It can be used both at declaration and assignment.
  • Useful in Method Calls: We can pass arrays directly like: numbers(new int[]{1, 2, 3});

Which one to use?

  • Use {…..} shorthand when declaring and initializing in single statement.
  • Use new int[]{….} when we need to assign later or pass arrays directly to methods.