3 minutes read
CHEAT SHEET: How To Initialize Arrays In Java

CHEAT SHEET: How To Initialize Arrays In Java

Arrays are best understood in Java as data structures that store a fixed-size sequential collection of elements of the same type. Put another way, arrays are used to store a collection of data, but you can also think of an array as a collection of variables of the same type. So how do you go about initializing arrays in Java to get the most out of the language? This guide offers step-by-step instructions on how to use this particular type of Java object.

How to Initialize Arrays In Java

An array is one of the most essential Java objects for programmers. By using them effectively, you can more easily sort variables and thus sort data for a program or application you are building. So what fundamentals do you need to know to start initializing arrays in Java? And what are the steps to doing that properly? Let’s dive in. 

An array is a container object that holds a fixed number of values of the same type.

Let’s first declare an array and then look at different ways of initializing it in Java.

To declare an array, you need to define the type of the elements with square brackets.
For example, to declare an array of integers:

int[ ] arr;

The name of the array is arr. The type of elements it will hold is int.

Note that elements in the array are identified with zero-based index numbers, meaning that the first element's index is 0 rather than one. So, the maximum index of the array int[5] is 4.

Initializing an Array

If you already know what values to insert into the array, you can use an array literal.
Example of an array literal:

String[ ] myNames = { "A", "B", "C", "D"};


// Outputs "C"

Try It Yourself 

Place the values in a comma-separated list, enclosed in curly braces.
The code above automatically initializes an array containing 4 elements, and stores the provided values.

Sometimes you might see the square brackets placed after the array name, which also works, but the preferred way is to place the brackets after the array's data type.

If you want to declare an array and insert the values later, the above syntax won’t work.
When assigning a new array to a declared variable, the keyword new must be used: 

String[ ] myNames;

myNames = new String[ ] { "A", "B", "C", "D"};

Try It Yourself

You can create an array of a predefined size and then initialize the array values using a for loop:

int[ ] arr = new int[5];

for (int i = 0; i < arr.length; i++) {

   arr[i] = i*42;


Try It Yourself

CHEAT SHEET: How To Initialize Arrays In Java by SoloLearn

Initializing Multidimensional Arrays

The same syntax works for multidimensional arrays.

For example:

int[ ][ ] arr = {{1,2,3},{2,4,5},{4,4,5}};  

// or

int[ ][ ] arr = new int[3][3];

Try It Yourself

Once you’ve done some practice and feel like you have mastered these steps (or other Java objects), the last step is to start putting them to use! If you still feel unsure with these or other elements of Java, our Java tutorial offers various tutorials, quizzes, and practice coding for you to grow more comfortable with them!