← Java Arrays & Strings · guided lesson

Arrays & Strings in Java

1 / 18 · story

the story

Java arrays (int[], int[][])

Every tool here exists because something was too slow or broke without it. Here is what.

1995 · James Gosling and the Sun Java team

In C, an array is just a pointer to memory, and nothing stops you writing past its end. Java's October 1995 white paper made arrays real objects and promised that every access is checked against the array's bounds. The Java Language Specification still says it: all array accesses are checked at run time, and a bad index throws ArrayIndexOutOfBoundsException. Fun fact: that 1995 white paper wrote myPoints.length() for arrays — the shipped language made it a plain field, length, the same mix-up learners make today.

the problem it solved

Out-of-bounds writes in C silently corrupt memory and crash much later (the white paper calls it the "delayed-crash" syndrome). Checked arrays turn that into an immediate, named exception.

why it matters: without it

Every count, table and grid would need a map or a list of objects: several times the memory and a pointer hop on every read.

sources: Gosling & McGilton, The Java Language Environment (white paper, Oct 1995), §2.1.3 Arrays ↗ · Java Language Specification (SE 17), Chapter 10: Arrays ↗

also in this chapter: java.util.Arrays helpers · Primitive vs boxed values · String: immutable and pooled · StringBuilder · char arithmetic