Skip to content

Java text — a learning library

Everything Java gets wrong, right, and surprisingly, about text. One idea per page. Every claim on every page is backed by a program that runs, and whose output is checked against a recorded answer key in CI — so nothing here is quoted from the Javadoc and hoped for.

Java's text model was designed in 1995, when Unicode fit in 16 bits. It hasn't since 1996. Most of what is confusing about Java strings follows from that one fact and the thirty-year compatibility promise built on top of it — plus a set of defaults chosen for a world where everyone's text was ASCII.

Start here

00 — Start here — what this library assumes, and the four things to read first.

The chapters

Chapter What it covers
01 char and String why length() is not the length, and what to use instead
02 Encodings the default charset, malformed input, the BOM
03 Locale the methods that change behaviour depending on where the JVM runs
04 Regex and splitting ASCII-only defaults, and split's three surprises
05 The language itself escapes that run before the compiler; running a file with no build tool
06 Concatenation and performance why the loop is quadratic, and what + really compiles to
07 Resources the JEPs, the specs, and the sibling libraries

Running the examples

You need a JDK, version 11 or later, and nothing else — no Maven, no Gradle, no IDE. Every example is a single file:

java 01_Char_and_String/length_is_three_numbers/examples/length_is_three_numbers_java.java

To run all of them and check every recorded output:

python3 tools/run_examples.py --check

Written against OpenJDK 25. Where a fact depends on the version, the page says which version.

The one rule

No page hand-types what a program prints. A lesson marks the spot and the runner fills it from a real run — so an example that changes behaviour on a new JDK breaks the build instead of quietly making a page wrong. See CONTRIBUTING.md.