How to Initialize an Array in Java: The Basics

Athena Ozanich
Athena Ozanich


Java is an object-oriented programming language which means everything in it is an object, from classes to variables and even arrays. Arrays are a powerful tool that changes how you can work with data that should is grouped.

Woman enjoying her success in learning about Java arrays and how to initialize them.

This post will cover initializing an array and the various ways to do so. You will see some code examples showcasing array initialization syntax using each approach. You will also learn about the caveats of array initialization using each of the methods discussed.

Without further delay, let’s get started.

Download Now: An Introduction to Java & JavaScript

What is a Java array?

In any programming language, an array is a collection of data stored within a variable. This array variable consists of multiple values stored together for later use. Arrays are a frequently used object in Java and can serve many purposes, such as storing text strings, numbers, boolean values, and other arrays.

The array object uses an index to track each element within it. The index of an element depends on its position in the array, and arrays start counting indexes at zero. Each element's location is one number higher than its index. So in an array, the first element is at index zero, the second at index one, etc.

Now that we’ve discussed the basic information about arrays let’s move on to initializing one.

Java Initialize Array

There are several ways to initialize arrays in Java; each approach comes with its own syntax and related caveats. This section will show some code examples explaining each technique and tips on avoiding invalid initialization errors. The video below illuminates how to create and initialize an array.

Declaring a Java Array Variable

The syntax for declaring a Java array at its most basic can be seen below. However, it is worth noting that declaring an array in Java does not initialize the array.

datatype[] arrayName;

The syntax above has three notable parts; the first is the datatype which is a placeholder for the datatype that the array will hold. Examples of acceptable datatypes are int, char, String, and Boolean, just to name a few.

The second notable part is the square brackets — [] — which indicates that the variable will hold an array object. Finally, the last part — arrayName — is simply the array's name.

Declare and Initialize Array

To use the declared array in your code, you will need to initialize it; this is where the fun starts. There are a few ways to initialize the array; the first is by using the new keyword. First, let’s look at an example of declaring and initializing an array of the primitive and object datatypes.

The syntactic difference between the two is the capital letter and full word format for the object datatype's keyword. Aside from that, the syntax is almost identical to that of the syntax for primitive datatype array initialization.

1. Primitive Datatype Array

int[] nums = new int[5];

The code above declares a primitive datatype array with five unspecified int values. The default value is different based on the datatype used to declare the array. In this example, the array will have five default values of zero.

Note: When initializing an array using the new keyword, you must specify the starting size of the array to avoid an error.

2. Object Datatype Array

String[] strArr = new String[4];

Initializing an array of the object datatype is similar to the above, except that the object datatype keywords look different from primitive datatype keywords. Above is a simple example of declaring and initializing an object datatype array. The array in this example would start with four default values of the String datatype, which is null.

Initialize Array After Declaration

You can also declare the array and initialize it later in your code. The syntax looks very similar to the previous method, except that the process can be broken into two steps. This is very useful when declaring an array before you have its values.

/* Declare */
int[] array;

/* Initialize */
array = new int[5];

Note: When initializing an array after its declaration, the new keyword must be used, or it will result in unpredictable behavior and or an error.

Multidimensional Array in Java

In Java, it is possible to create multidimensional arrays. This term is a misnomer, as they aren’t multidimensional arrays. Instead, they are arrays containing arrays, also known as nested arrays. Let's look at an example of the multidimensional array syntax below.

int[][] mdNums = new int[2][];

Note: When initializing a multidimensional array, you will need to declare the size of the first array in the variable to avoid an error.

To specify the size of one of the arrays within the multidimensional array, you can target the one you want to modify using the index just like you would on an array. Furthermore, just like a standard array in Java, you will need to initialize the entire array structure before using it in your code.

mdNums[1] = new int[3];

Initialize Java Array With Shorthand Syntax

Java also provides a shorthand syntax for declaring and initializing an array, which can simplify declaring and initializing arrays in your software. This syntax can create and initialize multidimensional arrays as well. Let’s look at that syntax in the code snippet below.

int[] nums = {1,2,3};

With the shorthand syntax, the array is created and immediately assigned values; the curly braces wrapped around the numbers indicate that the values within should be added as array elements. So the first array — nums — would consist of three array elements with the values one, two, and three — at indexes zero, one, and two.

Finally, the shorthand syntax for a multidimensional array is similar. However, it has its caveats as well. Primarily the multidimensional array does not require that each inner array be the same size which is called a symmetric matrix. This is because it is an array containing arrays. When initializing it, you will need to wrap your values in curly braces and then wrap those in braces.

To be thoroughly clear about this syntax, let’s look at the code snippet below.

int[][] mdNums = ;

This code creates an array containing two arrays, one with two values of one and two and the second with three values of one, two, and three.

Getting Started Using Java Arrays in Your Software

You have learned a lot about arrays in this post. You’ve learned several ways to create them and the syntax for each method. You have also discovered the caveats of each approach and how to avoid syntactic errors in your software. You can practice your understanding of arrays by creating several kinds of arrays for various purposes. Practice does make perfect, after all.


Topics: Java

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

Learn more about one of the world's most popular programming languages.