Java Arrays (with examples)

int[] arr = new int[5];

arr[0] = 5;
arr[3] = 2;

// arr == [ 5, 0, 0, 2, 0 ]
  • Arrays have fixed length. Once created, they can not be resized.
  • In an array of length 10, the first index is 0 and the last is 9.
  • Arrays are objects
    • They are dynamically allocated
    • They live on the heap
    • Array variables are of reference type

Arrays are to be considered a low level language feature. In 99 cases out of 100 you’ll want to use a List. See Java: Array vs ArrayList.

Three ways to create an array

// Short form
// (can only be used as initializer in a declaration)
int[] arr1 = { 1, 2, 77 };

// Initialize with 10 zeroes
int[] arr2 = new int[10];

// General form
int[] arr3 = new int[] { 1, 2, 77 };
Note: It's possible to put the brackets after the identifier:
int arr[] = { 1, 2, 3 };
This format is rarely used and discouraged by most style guides.

Accessing Elements

Since indexes start at 0, you write for example arr[2] to access the third element of arr. In an array with n elements, the last valid index is n-1.

If you try to access an element at a negative index or an index greater than or equal to the length of the array, an ArrayIndexOutOfBoundsException will be thrown.


To get the length of an array, arr, use arr.length.

The length is represented by an int which means the maximum length is 231-1 (Integer.MAX_VALUE).


Arrays can be used in enhanced for loops:

for (int element : arr) {

If you need to keep track of the index, use a traditional for loop:

for (int i = 0; i < arr.length; i++) {
    System.out.println("Index " + i + ": " + arr[i]);

For more details on loops, see: Java Trail: Loops


System.out.println(arr) prints something like [I@3af49f1c. Here’s a better way:

int[] arr = { 1, 2, 77 };

// Output: [1, 2, 77]

Primitives vs objects (references)

Arrays are layed out linearly in memory. In other words, an int[] looks like this:

int[]: 3 1 15 8 11

In Java, object variables actually store references (see Java: Primitives vs Objects and References), so for a Person[] array, the picture looks like this:

Person[]: name: John age: 19 name: Bob age: 55 name: Amy age: 25 name: Eve age: 29

The references are stored linearly, but the actual objects are spread out in memory.

Multidimensional arrays

Java does not have “true” multidimensional arrays. Instead you typically use arrays of arrays:

int[][] matrix = new int[3][5];
matrix[0][0] = 123;
matrix[2][4] = 321;

For more details see: Java: Matrices and Multidimensional Arrays