A Java String is carved in stone — every edit builds a new object. StringBuilder is the chisel: one mutable buffer you can grow, reverse and rewrite.
1 Immutable Strings
"cat".toUpperCase() does nothing on its own — the new string is discarded unless you assign it. s = s + "!" creates a fresh object.
2 Reads are cheap
s.charAt(i), s.length() and s.indexOf(x) are O(1) or a quick scan — reading never mutates anything.
3 The concat trap
s.concat(t) returns a new string. Ignore the return value and your change evaporates — the classic bug.
4 StringBuilder
A resizable char buffer. append() writes into spare capacity — amortized O(1) — and reverse() flips it in place.
5 Equality rules
s1 == s2 compares references; s1.equals(s2) compares characters. Use .equals for text, always.
Immutable vs builder
Immutable Strings1"cat".toUpperCase() does nothing on its own — the new string is discarded unless you assign it. s = s + "!" creates a fresh object. Reads are cheap2s.charAt(i), s.length() and s.indexOf(x) are O(1) or a quick scan — reading never mutates anything.
The concat trap3s.concat(t) returns a new string. Ignore the return value and your change evaporates — the classic bug. StringBuilder4A resizable char buffer. append() writes into spare capacity — amortized O(1) — and reverse() flips it in place. Equality rules5s1 == s2 compares references; s1.equals(s2) compares characters. Use .equals for text, always.
5 building blocks make up Immutable vs builder
One buffer, spare slots, zero copies — appends fill the gaps until a grow is unavoidable.
1 / 10A StringBuilder built from "Hi": size 2, capacity 4 — two spare slots waiting.
Two pointers walk inward and compare — "level" survives every check, "lever" dies at the middle.
process decision input / output start / end Counting vowels with charAt, then building and flipping a greeting with StringBuilder.
mainword = "education" — 9 characters, indexes 0..8.
Console
output appears as the program runs…
charAt, indexOf, substring — all read-only. Watch "banana" get examined without ever being touched.
1 / 10Strings are read-only treasure. We will scan, slice and read — and the original never changes.
Immutability strikes: one ignored return value, one honest reassignment, one buffer that plays by different rules.
Your predicted output — one line per console line
Hint: concat returns a NEW string — without assignment it vanishes. StringBuilder actually mutates, and reverse() flips in place.
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand ?immutabledrops into place
?.equals()drops into place
?==drops into place
?charAt(i)drops into place
?substring(2, 5)drops into place
?StringBuilderdrops into place
?reverse()drops into place
?concat trapdrops into place
01Strings are immutable: s + t and s.concat(t) both build new objects — assign the result or lose it.
02Use .equals() to compare text; == compares references and lies across objects.
03charAt, length and indexOf are cheap reads; substring copies — remember the exclusive end index.
04StringBuilder.append writes into spare capacity — amortized O(1), one copy per growth.
05reverse() mutates in place — calling it twice restores the original.
06Building a string in a loop? StringBuilder is not optional — it is the difference between linear and quadratic.