← Java Arrays & Strings · guided lesson
Arrays & Strings in Java
the story
Java arrays (int[], int[][])
Every tool here exists because something was too slow or broke without it. Here is what.
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
the idea
The idea in 30 seconds
An int[] is a fixed row of numbered slots. A String is a sealed row of characters: you can read it, never change it.
Almost every DSA answer reads, copies or builds one of these. Their costs and traps (length vs length(), ==, += in a loop) decide whether your code is O(n) or O(n²), right or wrong.
Here you learn the tools. The array patterns (prefix sums, Kadane, two pointers) come in Stage 3.
picture this
A String is a PDF someone sent you: you can read any character instantly, but you can't edit it — every "change" exports a whole new PDF. A StringBuilder is the Word file you keep typing into, then export once at the end.
watch it
s += c vs StringBuilder — count the copies
Predict mode is on. Before each step, guess what happens, then watch. Wrong guesses are fine; that is how it sinks in.
s += c vs StringBuilder — count the copies
Left bar: every `+=` builds a brand-new string. Right bar: the builder writes one char, and only copies when its array is full.
On this screen ← / → step the simulation. Use the buttons below to move through the lesson.
watch it
Char arithmetic — letters become array indexes
Predict mode is on. Before each step, guess what happens, then watch. Wrong guesses are fine; that is how it sinks in.
Char arithmetic — letters become array indexes
`c - 'a'` maps each letter to a counter. Pass 1 counts, pass 2 finds the first letter with count 1.
On this screen ← / → step the simulation. Use the buttons below to move through the lesson.
key idea 1 of 8
Read it once. Say it out loud once. Bas.
new int[n] gives n slots already filled with defaults: 0, false, '\u0000', or null for objects. The length is fixed forever.
key idea 2 of 8
Read it once. Say it out loud once. Bas.
Three different sizes: arr.length (a field, for arrays), s.length() (a method, for strings), list.size() (a method, for collections).
key idea 3 of 8
Read it once. Say it out loud once. Bas.
== on arrays or strings asks "is this the same object?". For contents use Arrays.equals(a, b) and s.equals(t). To print, use Arrays.toString(a).
key idea 4 of 8
Read it once. Say it out loud once. Bas.
int[] stores raw numbers side by side. Integer[] and List<Integer> store references to boxed objects — more memory, slower, and null is possible.
key idea 5 of 8
Read it once. Say it out loud once. Bas.
A String never changes. s += c copies the whole string every time → O(n²) in a loop. StringBuilder.append is O(1) amortised → O(n) total.
key idea 6 of 8
Read it once. Say it out loud once. Bas.
A char is a 16-bit number. c - 'a' turns 'a'..'z' into 0..25 and (char) ('a' + i) turns it back — that's how an int[26] replaces a HashMap.
key idea 7 of 8
Read it once. Say it out loud once. Bas.
int[][] is an array of row arrays: grid.length rows, grid[r].length columns in row r. Rows may differ in length (jagged).
key idea 8 of 8
Read it once. Say it out loud once. Bas.
Library calls are not free: Arrays.copyOf, toCharArray, substring and split all copy — O(n) each.
spot the pattern
When you see this, think this
"Only lowercase English letters"
int[26]withc - 'a'— no HashMap neededValues in a small range, like 1 ≤ nums[i] ≤ 100
A counting array indexed by the value:
new int[101]Return a string you build piece by piece
StringBuilder, then onetoString()"Words separated by one or more spaces"
trim()+split("\\s+"), or scan with an index
+3 more signals on the chapter page
watch out
A trap or two
Many wrong answers in interviews come from small slips like these, not from the hard part.
arr.length(),s.length,list.length— three wrong spellings. The compiler catches them, but under pressure they cost minutes.==on arrays or strings compares references.Arrays.equals(a, b)anda.equals(b)compare contents.
quick check
One question. No pressure.
For int[] a and String s, which line compiles?
now try
Your first real problem
You know enough to try this one. Stuck? Use the hints; that is what they are for.
First Unique Character in a String
open the problem →recall
Flip a few cards
Answer in your head first, then flip. Grade yourself honestly; the schedule brings back what you forget.
Array, String, List: how do you get the size of each?
Question 1: Array, String, List: how do you get the size of each?
done
Lesson complete
Chalo, that is the core. The full chapter has the rest: cost tables, Java details and more problems.