Java
Programming
Array Initialization
Coding Tutorial
Java Basics

How to initialize an array in Java?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In Java, an array is a container object that holds a fixed number of values of a single type. It is a key data structure that helps in organizing multiple items. The length of an array is established when the array is created and remains constant. Arrays are particularly useful when you intend to work with multiple or a list of values of the same datatype.

Methods of Initializing an Array in Java

1. Using Braces {}

This is perhaps the simplest way to initialize an array in Java. This method is helpful when you already know the elements your array will hold.

java
int[] array = {10, 20, 30, 40, 50};
String[] words = {"hello", "world"};

In this example, array is an integer array with predefined integer elements, and words is an array of strings.

2. Using the new Keyword

When you know the size of the array but not necessarily the elements at initialization, you can use the new keyword.

java
int[] newArray = new int[5];  // All elements default to 0

This code initializes newArray with five elements, all of which are initially set to zero.

3. Using a for Loop

If you want to initialize an array with values computed at runtime or using a specific formula, a for loop is useful.

java
1double[] doublesArray = new double[10];
2for (int i = 0; i < doublesArray.length; i++) {
3    doublesArray[i] = i * Math.PI;
4}

Here, doublesArray is populated with multiple of Pi values.

Multi-dimensional Arrays

You can also initialize multi-dimensional arrays in Java. These are arrays of arrays, where each element in the array itself another array.

java
1int[][] matrix = {
2    {1, 2, 3},
3    {4, 5, 6},
4    {7, 8, 9}
5};

Here, matrix is a 3x3 integer array, often used in mathematical computations or games like tic-tac-toe.

Dynamic Array Initialization

Sometimes, you may need to initialize arrays based on some dynamic conditions. Here’s where ArrayList comes in, which is part of Java’s collection framework.

java
ArrayList<Integer> dynamicArray = new ArrayList<Integer>();
dynamicArray.add(5);
dynamicArray.add(10);

Unlike traditional arrays, ArrayLists are dynamic in size and can accommodate more elements.

Table for Summary of Array Initialization Methods in Java

MethodCode ExampleUse-case
Braces &#123;&#125;int[] array = &#123;1, 2, 3&#125;;Known fixed values
new Keywordint[] newArray = new int[5];Known size, unknown values
for Loopfor (int i = 0; i < size; i++)Values determined during execution
Multi-dimensionalint[][] matrix = &#123;...&#125;Arrays within arrays, for complex data
Dynamic (ArrayList)ArrayList<Integer> dynArraySize may vary, or not determined initially

Points to Note

  • Array indices start at 0, which means the first element of the array is accessed by array[0].
  • Java arrays are of fixed size. Once declared, their size cannot be modified. This is one reason why collections like ArrayList can be more flexible.
  • Initializing an array with &#123;&#125; or with the new keyword automatically assigns default values (like 0 for int, false for boolean, and null for object references).

Conclusion

Array initialization in Java is fundamental and dictates the efficiency and readability of your code. Depending on your specifics needs — whether the size and content are known at compile time, or need to be decided at runtime — different methods of array initialization can be used. Each method has its appropriate use cases and learning to use them proficiently can improve the performance and flexibility of your Java applications.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.