Java StringBuilder appendCodePoint() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.StringBuilder

Definition

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).

Java
StringBuilder sb = new StringBuilder("Hi ");
sb.appendCodePoint(65)         // appends 'A' (1 char)
  .appendCodePoint(0x1F600);   // appends U+1F600 (2 UTF-16 chars)
CallWhat it does
sb.appendCodePoint(65)Appends 'A' instead of the digits "65".
sb.appendCodePoint(0x1F600)Appends the two-char UTF-16 surrogate pair for U+1F600.
sb.appendCodePoint(cp)Chains because it returns this.
sb.appendCodePoint(-1)Throws IllegalArgumentException for an invalid code point.

Use Character.isValidCodePoint(cp) to validate untrusted numbers before passing them to appendCodePoint().

Library declarations
Java
// java.lang.StringBuilder
public StringBuilder appendCodePoint(int codePoint)

Parameters

ParameterDescription
codePoint (int)A valid Unicode code point in the range 0x0000 to 0x10FFFF.

Return Value

Return ValueDescription
StringBuilderA reference to this builder so further calls can chain.

Compare with Similar Methods

Appending and inspecting Unicode characters:

MethodHow it differs
append(int)Appends the decimal digits of the integer rather than a Unicode symbol.
codePointAt(index)Reads back the full code point starting at a UTF-16 index.
codePointCount(start, end)Counts whole code points rather than raw UTF-16 char units.
Character.charCount(cp)Tells whether a code point takes 1 or 2 char units in UTF-16.

Java Examples

Example 1 — Compare appendCodePoint() and append(int)

Pass the exact same integer codes to both methods to see characters versus decimal digits.

Java
public class StringBuilderAppendCpVsInt {
    public static void main(String[] args) {
        StringBuilder letters = new StringBuilder();
        letters.appendCodePoint(74).appendCodePoint(97).appendCodePoint(118).appendCodePoint(97);

        StringBuilder digits = new StringBuilder();
        digits.append(74).append(97);

        System.out.println("appendCodePoint: " + letters);
        System.out.println("append(int):     " + digits);
    }
}

How It Works

  1. Decode to letters. Calling appendCodePoint with 74, 97, 118, and 97 writes 'J', 'a', 'v', and 'a'.
  2. Digits for append(int). Passing 74 and 97 to append formats them as decimal text, yielding 7497.
  3. Fluent chaining. Both methods return this, so every call operates on the same builder instance.

Example 2 — Append a Supplementary Code Point as a Surrogate Pair

Add 0x1F600 and check both UTF-16 length() and codePointCount() without relying on console font rendering.

Java
public class StringBuilderAppendCpSurrogate {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hi");
        sb.appendCodePoint(0x1F600);
        sb.appendCodePoint('!');

        System.out.println("UTF-16 length = " + sb.length());
        System.out.println("codePointCount = " + sb.codePointCount(0, sb.length()));
        System.out.println("cp at 2 = 0x" + Integer.toHexString(sb.codePointAt(2)).toUpperCase());
        System.out.println("last char = " + sb.charAt(sb.length() - 1));
    }
}

How It Works

  1. Two units added. Starting with "Hi" (2 chars), appendCodePoint(0x1F600) appends a high and low surrogate pair, plus '!' gives length 5.
  2. Four logical characters. codePointCount(0, 5) treats the surrogate pair at indexes 2 and 3 as one code point, returning 4.
  3. Round-trip verified. codePointAt(2) reassembles the two surrogates back into 0x1F600.

Example 3 — Validate Code Points and Catch IllegalArgumentException

Guard candidate integers with Character.isValidCodePoint and handle out-of-range values safely.

Java
public class StringBuilderAppendCpInvalid {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Tag:");
        int[] candidates = {65, 0x03A9, -1, 0x110000};

        for (int cp : candidates) {
            if (Character.isValidCodePoint(cp)) {
                sb.appendCodePoint(cp);
            } else {
                try {
                    sb.appendCodePoint(cp);
                } catch (IllegalArgumentException e) {
                    System.out.println("Rejected invalid code point: " + cp);
                }
            }
        }
        System.out.println("length = " + sb.length());
        System.out.println("last hex = 0x" + Integer.toHexString(sb.codePointBefore(sb.length())).toUpperCase());
    }
}

How It Works

  1. Check range first. 65 ('A') and 0x03A9 (Greek Omega) pass Character.isValidCodePoint and append one char each.
  2. Invalid values throw. Negative -1 and 0x110000 (1114112, just above 0x10FFFF) both trigger IllegalArgumentException.
  3. 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.

Next: capacity()

Return how many char units the internal buffer can hold before allocating a larger array.

capacity() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful