Java Arrays

An array is a container that holds a fixed number of values of the same type. Instead of creating a separate variable for every value, you group them under a single name and reach each one by its position number, called an index.

What is an array?

Imagine you need to store the scores of four students. You could create four separate variables, but that quickly becomes messy. An array lets you keep all four values together in one place and pick out any single value whenever you need it. In Java, every element in an array must share the same data type, so an int array holds only whole numbers, and a String array holds only text.

Declaring and initializing an array

public class Main {
  public static void main(String[] args) {
    // Create a String array with four values
    String[] fruits = {"Apple", "Banana", "Cherry", "Mango"};

    // Create an int array the same way
    int[] scores = {88, 72, 95, 60};

    System.out.println(fruits[0]); // Apple
    System.out.println(scores[2]); // 95
  }
}

The square brackets after the type tell Java that the variable holds an array rather than a single value. You can write the brackets after the type (int[] scores) or after the name (int scores[]) — both compile, but placing them after the type is the style most Java developers prefer.

Accessing and changing elements

Array indexes start at 0, not 1. This means the first element sits at index 0, the second at index 1, and so on. To read an element you write the array name followed by the index in brackets. To change an element, you assign a new value to that same position.

Reading and updating values

public class Main {
  public static void main(String[] args) {
    String[] colors = {"Red", "Green", "Blue"};

    // Change the second element
    colors[1] = "Yellow";

    System.out.println(colors[1]); // Yellow
    System.out.println(colors[0]); // Red
  }
}

Array length

Every array knows how many elements it holds. You can read this count using the .length property. Notice that length is a field, not a method, so you never write parentheses after it. Because indexes start at 0, the last valid index is always length minus one.

Using .length

public class Main {
  public static void main(String[] args) {
    int[] numbers = {10, 20, 30, 40, 50};

    System.out.println("This array has " + numbers.length + " elements.");
    System.out.println("Last element: " + numbers[numbers.length - 1]); // 50
  }
}
  • Arrays have a fixed size — once created, you cannot add or remove slots.
  • All elements must be the same type.
  • The first index is 0; the last index is length - 1.
  • Use .length (no parentheses) to find out how many elements exist.
TaskSyntaxExample
Declare with valuestype[] name = {...};int[] a = {1, 2, 3};
Read an elementname[index]a[0]
Change an elementname[index] = value;a[0] = 9;
Count elementsname.lengtha.length
Note: Trying to reach an index that does not exist, such as a[5] in an array of three elements, throws an ArrayIndexOutOfBoundsException and stops your program. Always stay between 0 and length - 1.

Exercise: Java Arrays

What happens when code accesses an array index that does not exist, such as arr[10] on a 5-element array?