liveBook · Manning

Codepoint-centered api

As explained in 100 Java Mistakes and How to Avoid Them

A codepoint-centered API is an interface that represents and processes text using Unicode code points rather than individual UTF-16 code units.

A codepoint-centered API processes text as Unicode code points instead of individual UTF-16 char values. This avoids misreading characters outside the Basic Multilingual Plane, which may occupy surrogate pairs. Prefer String methods such as codePointAt(), codePointBefore(), codePointCount(), and codePoints(), plus StringBuilder.appendCodePoint(). section 6.1

Replace char-based digit checks

A char-based check fails for a digit represented outside the Basic Multilingual Plane: static boolean startsWithDigit(String str) { return !str.isEmpty() && Character.isDigit(str.charAt(0)); } Use the int overload with a code point instead: static boolean startsWithDigit(String str) { return !str.isEmpty() && Character.isDigit(str.codePointAt(0)); } The int overload handles characters that char-based methods cannot represent. section 6.1

String part = string.codePoints().skip(rowNumber * rowLength)
       .limit(rowLength)
       .collect(StringBuilder::new,
                StringBuilder::appendCodePoint,
                StringBuilder::append)
       .toString();

Extracts a string portion by traversing code points and rebuilding it with StringBuilder.appendCodePoint(). section 6.1

Use the technique with these cautions:

  • Avoid char-accepting APIs in public interfaces when arbitrary characters are possible. section 6.1
  • Remember that codePointAt(index) accepts a char index, not an ordinal codepoint position. section 6.1
  • Expect more verbose traversal and potentially slower nth-codepoint access than charAt() or substring(). section 6.1

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains surrogate-pair failures, corrected overload selection, traversal costs, and codePointAt indexing limits.
Examples
Uses non-Basic-Multilingual-Plane digits, emoji, Chinese names, digit checks, and codepoint-based string extraction.
What it assumes
Assumes Java String and char familiarity while teaching why UTF-16 char values do not always equal characters.
What you can do after
Apply it via Java's codepoint-aware String and StringBuilder methods to process non-BMP characters safely.
How it connects
Connects Unicode code points with UTF-16 surrogate pairs, String APIs, Character.isDigit overloads, and the Stream API.

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.