Lesson · Strings & Text
Character encoding: ASCII, UTF-8, UTF-16 and surrogates
12 min readCharacter Encoding & Unicode
Chars are not characters: Java's char is a UTF-16 code unit, so one visible glyph can be two of them. The encoding story from ASCII to emoji.
From 128 to 155,000 and counting
ASCII spent 7 bits mapping English letters, digits and punctuation, 128 code points total. Every non-English script needed its own incompatible extension, and mixed-language text was unrepresentable. Unicode replaced the pile with one repertoire: a code point per character, from U+0000 to U+10FFFF, encoding-free by design.
Encodings turn code points into bytes. UTF-8 stores ASCII in one byte and anything else in two to four, which is why it dominates the web. UTF-16 stores most in two bytes and the rare rest in four, and it is what the JVM uses internally, which is the fact that makes Java's char a lie: it is a UTF-16 code unit, not a character.
Surrogate pairs: when one glyph is two chars
UTF-16 covers the first 65,536 code points in one 16-bit unit. Code points beyond (emoji, CJK extensions, historical scripts) are encoded as a pair of units from a reserved block: the high surrogate (U+D800 to U+DBFF) followed by the low surrogate (U+DC00 to U+DFFF). The pair represents one code point; the units alone represent nothing valid.
The consequences land in ordinary code. An emoji string has length() 2, substring(0, 1) can split it into half a character, and a reverse loop over chars produces garbage. codePointCount gives the real character count; codePoints() streams actual code points; offsetByCodePoints navigates by them. Java chose UTF-16 for historical reasons (fixed-width units were 1995's idea of simple); the price is surrogates.
Code units vs code points · "a 😀 b"
The string on screen is "a, emoji, b": three glyphs a human would count. To Java it is four chars, and this view is about where the extra one hides.
Charsets at the boundaries
Strings and bytes meet only through a charset: str.getBytes(UTF_8) and new String(bytes, UTF_8). Since JDK 18 the default charset is UTF-8 everywhere; before that, the platform's charset (cp1252 on Windows) made 'works on my machine' a genuine encoding phenomenon. State the charset explicitly at every boundary anyway: files, sockets, databases.
Reading a file with the wrong charset produces mojibake: text decoded into plausible-looking garbage. é where é was meant is the signature of UTF-8 byte pairs read as Latin-1. The fix is never re-encoding characters; it is declaring the same charset on both sides.
Even Unicode has equality problems
One visible character can have several code point sequences: é as a single unit (U+00E9) or as e plus a combining accent (U+0065 U+0301). equals() compares units, so the two forms are unequal strings that look identical on screen. Normalisation (NFC composition, NFD decomposition) rewrites one into the other before comparing.
The practical rules: normalise user input to NFC before storing or comparing, never trim with char arithmetic on international text, and let the libraries (Normalizer, Collator for locale-aware ordering) do the language-sensitive work. English-only habits fail quietly on the first international string.
Emoji have length 2, and substring can end halfway through one
Any character above U+FFFF is two chars in Java: an emoji, a rare CJK glyph, a musical symbol. length() counts units, indexes land on them, and substring(0, 1) can produce an unpaired surrogate that prints as a replacement symbol. When text is international or user-supplied, navigate with code points, not indexes.
Interview angles
What is Java's char, exactly?+
A 16-bit UTF-16 code unit, not a character. Most BMP characters map one-to-one, but supplementary characters (emoji, rare scripts) need a surrogate pair and therefore two chars. length() counts the units.
UTF-8 versus UTF-16?+
UTF-8 is a byte encoding: ASCII takes one byte, others two to four, ideal for I/O and storage. UTF-16 is the in-memory JVM form: two bytes per unit, four via surrogate pairs for supplementary characters. Same repertoire, different trade-off, both Unicode.
How do you count characters in a Java string correctly?+
str.codePointCount(0, str.length()) for a count that treats surrogate pairs as one, and str.codePoints() to stream them. Also consider grapheme clusters for user-visible glyphs (flags, family emoji), which is a further layer libraries like ICU handle.