Arrays in Java
Fixed-size, typed collections — how to declare, fill, and iterate over Java arrays, and what happens when you go out of bounds.
2 min read
An array is Java's most basic way to hold multiple values of the same type together, under one variable. Unlike the more flexible collections you'll meet later in this course, arrays have a fixed size set at creation time — that fixed size is exactly what makes them fast.
Declaring and initializing arrays
int[] numbers = new int[5]; // 5 ints, all default to 0
int[] scores = {90, 85, 77, 60}; // size 4, initialized with values
String[] names = new String[3]; // 3 Strings, all default to nullThe [] after the type marks it as an array of that type. When you create an array with new type[size] and no values, every slot is filled with a default: 0 for numeric types, false for boolean, and null for any object type (including String).
Accessing and modifying elements
Arrays are zero-indexed — the first element is at index 0, and the last is at length - 1.
int[] scores = {90, 85, 77, 60};
System.out.println(scores[0]); // 90
scores[1] = 95; // overwrite the second element
System.out.println(scores.length); // 4 -- note: no parentheses, it's a field, not a methodAccessing an index outside the valid range throws an ArrayIndexOutOfBoundsException at runtime — Java doesn't silently return null or wrap around like some languages do.
int[] scores = {90, 85, 77};
System.out.println(scores[5]); // throws ArrayIndexOutOfBoundsExceptionIterating over an array
int[] scores = {90, 85, 77, 60};
for (int i = 0; i < scores.length; i++) {
System.out.println("Index " + i + ": " + scores[i]);
}
for (int score : scores) {
System.out.println(score);
}Use the classic loop when you need the index; use for-each when you only care about the values.
Multidimensional arrays
Java supports arrays of arrays, most commonly used as a 2D grid:
int[][] grid = {
{1, 2, 3},
{4, 5, 6}
};
System.out.println(grid[1][2]); // 6 -- row 1, column 2Arrays are fixed size
Once created, an array's length never changes — there's no .add() or .remove(). If you need to grow or shrink a collection dynamically, that's exactly the gap ArrayList (from the Collections Framework, covered later) fills. Arrays still matter, though: they're the underlying storage for many collection types, and they're faster and lighter-weight when you know your size up front and it won't change.
The Arrays helper class
The java.util.Arrays class provides handy static methods that arrays don't otherwise have built in, like printing, sorting, and comparing:
import java.util.Arrays;
int[] scores = {90, 85, 77, 60};
System.out.println(Arrays.toString(scores)); // [90, 85, 77, 60]
Arrays.sort(scores);
System.out.println(Arrays.toString(scores)); // [60, 77, 85, 90]Without Arrays.toString(), printing an array directly with System.out.println(scores) prints an unhelpful internal reference string rather than the contents — that's one of the more common early surprises when working with arrays.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.