How do I declare and initialize an array in Java?

Gareth D.

The Problem

How do I declare and initialize an array in Java?

The Solution

The easiest way to declare and initialize an array of a primitive type such as int in Java is by using the following syntax.

int[] myArray = new int[]{1, 2, 3};

This does a few things at once.

  • The int[] myArray says that we want to declare a new variable called myArray that has the type of an array of integers.
  • new int[] says we want to immediately allocate memory to store data in this array.
  • {1,2,3} is the data we want to store in the array. Java will count the elements in our initial array and automatically initialize the array with enough memory for three ints.

Here’s a runnable example:

class Main { public static void main(String[] args) { // declare the variable and allocate memory int[] myArray = new int[]{1, 2, 3}; for (int i = 0; i < 3; i++) { System.out.println(myArray[i]); } } }

Which will print out:

1 2 3

If you don’t want to hard code the data immediately when you initialize the array, you can instead create an empty array (with all values initialized to zero) as follows.

int[] myArray = new int[3];

Here, you have to explicitly give the size of the array. In our example, we’ve created an array that can store 3 ints, all of which are zero.

Here’s a runnable example of dynamically setting the values in an array.

class Main { public static void main(String[] args) { // declare the variable and allocate memory int[] myArray = new int[3]; myArray[1] = 100; for (int i = 0; i < 3; i++) { System.out.println(myArray[i]); } } }

Which will print:

0 100 0

Other Types of Arrays

You can use the same syntax to create arrays to store other objects. In the example below, we use Integer instead of int, which is very similar to the above code but initializes the default values to null instead of 0.

class Main { public static void main(String[] args) { // declare the variable and allocate memory Integer[] myArray = new Integer[3]; myArray[1] = 100; for (int i = 0; i < 3; i++) { System.out.println(myArray[i]); } } }

The code prints:

null 100 null

Initialize a two-dimensional (2D) array in Java

You can create a 2D array in Java by adding a second set of square brackets, for example:

int[][] myArray = new int[4][2];

This will create an array with four rows and two columns. Here’s a runnable example:

class Main { public static void main(String[] args) { // declare the variable and allocate memory int[][] myArray = new int[4][2]; myArray[2][1] = 1; for (int i = 0; i < 4; i++) { for (int j = 0; j < 2; j++) { System.out.print(myArray[i][j] + " "); } System.out.println(); } } }

This prints the following (we set the value of the third row and second column to 1, and the rest of the values all have the default 0).

0 0 0 0 0 1 0 0

Initialize a Multidimensional Array in Java

You can initialize more dimensions by adding more square brackets, for example:

int[][][] myArray = new int[4][2][3];

Arrays with three or more dimensions are harder to visualize, but here’s a runnable example that prints it out as four sets of 2-by-3 arrays.

class Main { public static void main(String[] args) { // declare the variable and allocate memory int[][][] myArray = new int[4][2][3]; myArray[2][1][2] = 1; for (int i = 0; i < 4; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 3; k++) { System.out.print(myArray[i][j][k] + " "); } System.out.println(); } System.out.println("____"); } } }

This will print:

0 0 0 0 0 0 ____ 0 0 0 0 0 0 ____ 0 0 0 0 0 1 ____ 0 0 0 0 0 0 ____

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

Share on Twitter
Bookmark this page
Ask a questionJoin the discussion

Related Answers

A better experience for your users. An easier life for your developers.

    TwitterGitHubDribbbleLinkedinDiscord
© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.