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.
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.
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.
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.
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.
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.
Unlike traditional arrays, ArrayLists are dynamic in size and can accommodate more elements.
Table for Summary of Array Initialization Methods in Java
| Method | Code Example | Use-case |
Braces {} | int[] array = {1, 2, 3}; | Known fixed values |
new Keyword | int[] newArray = new int[5]; | Known size, unknown values |
for Loop | for (int i = 0; i < size; i++) | Values determined during execution |
| Multi-dimensional | int[][] matrix = {...} | Arrays within arrays, for complex data |
| Dynamic (ArrayList) | ArrayList<Integer> dynArray | Size 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
{}or with thenewkeyword automatically assigns default values (like 0 forint,falseforboolean, andnullfor 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
- How to initialize an array's length in JavaScript?
- How to initialize HashSet values by construction?
- How to initialize List<String> object in Java?
- How to input a list of lists with different sizes in tf.data.Dataset
- How to inject active spring profile into logback
- How to inject config properties in Spring Boot to Spring Retry annotation?
- How to interpret a Java thread stack?
- How to intersect two sorted integer arrays without duplicates?

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 courseTrack 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.