In Java, sb.appendCodePoint(codePoint) converts an int Unicode code point into its UTF-16 character representation and appends it to the end of the StringBuilder.
For characters in the Basic Multilingual Plane (0x0000 to 0xFFFF), it adds one char. For supplementary characters (0x10000 to 0x10FFFF, such as most emoji), it adds two chars forming a surrogate pair, and returns this for chaining.
An instance method of the java.lang.StringBuilder class (since Java 1.5, JDK 1.5): a final, mutable sequence of UTF-16 characters that is not thread-safe. Call it on an existing builder, as in sb.appendCodePoint(...). No import is needed because java.lang is always visible.
Syntax
Pass any valid Unicode code point from 0 to 0x10FFFF (Character.MAX_CODE_POINT).
Check range first.65 ('A') and 0x03A9 (Greek Omega) pass Character.isValidCodePoint and append one char each.
Invalid values throw. Negative -1 and 0x110000 (1114112, just above 0x10FFFF) both trigger IllegalArgumentException.
Verify buffer state. After appending two BMP code points onto "Tag:", total length is 6 and the ending code point is 0x3A9.
Frequently Asked Questions
append(int) writes the integer as decimal digits (65 becomes "65"). appendCodePoint(int) writes the Unicode character for that code point (65 becomes "A").
It adds 1 char for code points from U+0000 to U+FFFF, and 2 chars (a UTF-16 surrogate pair) for supplementary code points from U+10000 to U+10FFFF.
It throws IllegalArgumentException if codePoint is less than 0 or greater than Character.MAX_CODE_POINT (0x10FFFF).
Yes. Like append(), it returns a reference to this StringBuilder.
A char widens cleanly to int, so sb.appendCodePoint('!') works for BMP characters, though sb.append('!') is simpler for ordinary single chars.