Introduction to Arrays
Introduction to Arrays
So far every variable you have declared holds a single value — one int, one String, and so on. But what if you need to store the scores of thirty students, or the names of every day in the week? Creating thirty separate variables would be tedious and unmanageable. Arrays solve this by grouping multiple values of the same type under a single name.
What Is an Array?
An array is a fixed-size, ordered collection of values that all share the same data type. Think of it as a row of numbered boxes: each box holds one value, and each box has a position number called an index.
- Every element in an array must be the same type (e.g. all
int, allString). - The size of an array is fixed at the moment it is created — you cannot shrink or grow it later.
- Indexes start at 0, not 1.
Declaring an Array
The declaration syntax places square brackets either after the type or after the variable name. The idiomatic Java style places them after the type:
Declaring the variable does not yet create the array — it just reserves a name that will point to an array once one is created.
Initializing an Array
There are two common ways to give an array its values.
1. Allocate with new, then assign element by element
Use new int[5] to create an array that can hold exactly 5 integers. Java fills all slots with the default value for the type — 0 for numeric types, false for boolean, and null for objects.
2. Array initialiser (declare and fill at once)
When you already know the values, you can provide them in curly braces. Java counts the values and sets the size automatically.
Reading the Length of an Array
Every array exposes a length field (not a method — no parentheses) that returns the number of slots it was created with:
length always reflects the capacity of the array, not the number of non-zero or non-null elements. An array created as new int[10] always has length equal to 10, even if you only filled three slots.
Accessing and Modifying Elements
Read or write any element using its index in square brackets:
ArrayIndexOutOfBoundsException
The most common array mistake is using an index that does not exist — either negative or equal to (or greater than) the array length. Java throws an ArrayIndexOutOfBoundsException at runtime when this happens, and your program stops unless you handle it.
n elements, the last valid index is n - 1. Accessing array[array.length] is always out of bounds. When in doubt, double-check your index against array.length - 1.
The safe pattern is to always use length when computing the last valid index, rather than a hard-coded number:
A Complete Example
The following snippet ties everything together: declare, initialise, read the length, access elements, and update a value.
Summary
Arrays let you store many values of the same type under one name. Declare them with type[], create them with new type[size] or an initialiser, and access elements with a zero-based index. Use .length to query the size, and always keep your indexes between 0 and length - 1 to avoid ArrayIndexOutOfBoundsException. The next lesson builds on this foundation by showing you how to loop over arrays and apply common manipulations.