Please enable JavaScript to use CodeHS

Arrays in Java

Learn how to store data in increasingly complex ways!

By Evelyn Hunter

Storing a piece of information in Java is a relatively simple process. For example, if we wanted to store the name of our favorite basketball player, we could do so by storing the name in a variable:

String favPlayer = "Steph Curry";
Plain text

But what if you were asked to store the name of your favorite player on each team? It’s possible to store each of those values in a separate variable, but that process would be hard to keep track of! What would you name all of the variables? favplayer25? There needs to be a better way to store all of this information so that it can be easily accessed and organized. Luckily, arrays are one such way that we can store data more effectively.

An array is a list of data. Arrays can store many different types of information. In our current example, we can create a list of all of our favorite players on each team:

String[] favPlayers = {"Steph Curry", "James Harden", "Jayson Tatum", "Luka Doncic"};
Plain text

To create an array in Java, you must first know what type of data you want to store and how many elements the array will hold. In the player list example above, we created an array of Strings, but it’s just as easy to create arrays of ints or any other data type. Arrays can store both primitive and object data types.

Arrays can be created in a few ways:

Initializing an Array with Data

The easiest way of putting data into an array is to initialize the array with data when declaring it. To do so, simply include the array elements between curly braces. Here’s a list of 6 numbers:

int[] numberList = {10,20,30,40,50,60};
Plain text

Initializing a New Array

It’s also possible to declare a new empty array and add in the list items later. The format for creating an array is:

type[] arrayName = new type[numberOfElements];
Plain text

Let’s take apart the array declaration:

  • type[]: This tells us what type of data is being stored in the array. For example, a list of integers would be int[]. The brackets [] indicate that this is an array and not a single int.

  • arrayName: The name by which the array will be known. It’s best to pick a descriptive name, like groceryList instead of just things.

  • new: The new keyword indicates that a new array is being created.

  • type[numberOfElements]: The type of elements is mentioned again, as well as the number of items the array will store. For example, if an array int[6] were created, the array would store six int values. Once an array has been created, the number of items that can be stored in the array cannot change. This array will always store 6 items - no more, no less!

Setting Array Values

Elements in an array can be accessed using their index position in the array. Let’s create a new empty array of numbers, then add in two numbers:

// Create an empty array that will store 6 integers
int[] newNumberList = new int[6];

// Add the number 11 to index position 0
newNumberList[0] = 11;

// Add the number 13 to index position 1
newNumberList[1] = 13;
Plain text

The array now contains two integers:



You’ll notice that even though the array was created without assigning any values, the rest of the indices currently store the value 0. When creating arrays with default values, Java assigns a specific value for each item in the array depending on the type. Integers and doubles default to zero, booleans default to false, and objects (including Strings) default to null.


Type Default Value
int 0
double 0.0
boolean false
Objects null



The [] syntax can also be used to change elements in a list. Let’s change 13 to 21:

newNumberList[1] = 21;
Plain text

Here is an example program for you to examine. Try changing the value of the items in the array - how does that affect the order of the items in the list? Try choosing a value greater than 4 to put the String value - what happens when you do that?

Getting Array Values

Accessing elements in a list uses a similar syntax. We can get and store the number at index 0 into a variable:

// This variable will contain the number 11
int firstNumber = newNumberList[0];

// Two ways to print out the number 11 to the console
System.out.println(firstNumber);
System.out.println(newNumberList[0]);

// Now print out the number at index position 1 to the console
System.out.println(newNumberList[1]);
Plain text

The output from the code will be:

11
11
21
Plain text

Practice

Now that you’ve learned about how to create and access arrays, let’s try making one! In the following code editor, create a list of your five favorite TV shows. Then, print out your favorite of those five by accessing the element in the array and printing out the value: