liveBook · Manning

Surrogate pair

As explained in 100 Java Mistakes and How to Avoid Them

A surrogate pair is two UTF-16 code units that together encode one Unicode code point outside the Basic Multilingual Plane.

A surrogate pair is two Java char values that together represent one Unicode code point outside the Basic Multilingual Plane. Therefore, char-by-char processing and String.length() do not always match the characters users see. Many emoji require surrogate pairs. section 6.1

Splitting an emoji

A fixed-width substring can cut the pair for an emoji such as code point 0x1f601, leaving only � or �. The output may show a placeholder character instead of the emoji. Emoji with modifiers create a second boundary problem: a waving hand with a dark skin tone modifier occupies four Java chars, so a split can discard the modifier even when it avoids cutting either pair. section 6.1

Avoid treating UTF-16 code units as complete characters:

  • For user-facing chunks, account for combining characters as well as surrogate pairs; BreakIterator can locate character boundaries. section 6.1
  • Review abbreviations, row wrapping, and validation code for substring or charAt assumptions rather than relying on String.length(). section 6.1

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains surrogate encoding, split consequences, combining-character complications, and code-point-aware remedies.
Examples
Uses emoji splitting, skin-tone modifiers, substring chunking, code-point streams, and BreakIterator boundaries.
What it assumes
Assumes familiarity with Java char, String, substring, and indexing while clarifying their limits for Unicode text.
What you can do after
Diagnose and prevent Unicode corruption by replacing char-based boundaries with code-point or character-boundary processing.
How it connects
Connects Java char and String operations with Unicode code points, the Basic Multilingual Plane, emoji, and combining characters.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.