Lesson · Arrays
One-dimensional arrays: fixed size, indexed access
11 min readOne-dimensional arrays
Arrays are the one built-in data structure in Java: a fixed-length, indexed, zero-based block. Declaration, initialisation, default values, iteration and the bounds contract.
Creating an array
An array is declared by type plus [], then created with a length. int[] scores = new int[5]; allocates five int slots, all initialised to 0. The array literal form fills them at creation: int[] primes = { 2, 3, 5, 7 }; and its long form new int[] { 2, 3, 5, 7 } when the length must be decided at the assignment site.
The Java convention puts the brackets on the type (int[] a), not the name (int a[]), because the type of a is array-of-int and the declaration should say so. Both compile; the convention exists so int[] a, b; means both are arrays, which the old style makes ambiguous (b is only an int there).
Defaults and length
new int[5] fills zeros; new boolean[5] fills false, new String[5] fills nulls, new double[5] fills 0.0. The defaults are the same ones fields get, applied because array creation is allocation. Reading an element before you assign it is therefore a value, not an error: a null in a String array fails later, at the point of use.
Length is fixed at creation and read via array.length, a field, not a method. array.length() does not compile; the parentheses bug is worth burning in once. length - 1 is the last valid index, and every loop should respect the half-open convention from the loops topic: i < array.length.
The bounds contract
Java checks array indices at runtime: any access outside 0 to length - 1 throws ArrayIndexOutOfBoundsException. The exception is a feature, C arrays would happily read a neighbouring object's memory, and the JIT can eliminate the check when its analysis proves the index safe, so correctness costs nothing extra in hot code.
Negative indices are equally fatal, and the exception message names the offending index. When the trace says Index 5 out of bounds for length 5, the loop boundary is the first place to look: i <= length where the code meant i < length produces exactly that.
Iteration and the shape of array code
for-each reads an array without index arithmetic and is the default for scans and sums. Indexed loops appear when the position matters: fill by formula, swap two ends, compare neighbours (a[i] with a[i + 1]). The two-pointer pattern lives here: left and right walking toward each other, reversing or checking a palindrome.
Arrays are the one structure where the compiler knows the length; everything else in the chapter about arrays-internals and the Arrays utility builds on this base. Later collections, ArrayList above all, are grown-up wrappers around exactly this block.
length is a field, not a method
array.length reads a final field stamped into the object header; String.length() and List.size() are methods. Three ways to ask the same question, and the parentheses are not optional in the right places. Writing array.length() does not compile, which is the kind version of the mistake.
Interview angles
What are arrays initialised to?+
Numeric types zero, boolean false, char '\u0000', references null. The defaults come from allocation, the same values fields receive. Object arrays full of nulls compile fine and fail at the point of use instead.
length versus length() versus size()?+
Arrays expose the final field length; String exposes the method length(); collections expose size(). The differences are historical but consistent. array.length() is a compile error, not a subtle bug.
Why does Java check array bounds?+
Memory safety: out-of-range access is an exception, not a read of adjacent memory. The JIT removes provably safe checks, so the guarantee usually costs nothing at runtime.