Java StringBuilder codePointAt() Method

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

Definition

In Java, sb.codePointAt(index) returns the int Unicode code point starting at UTF-16 position index inside a StringBuilder.

When index holds a high surrogate followed by a low surrogate, it combines both char units into the full supplementary code point (such as 0x1F600) instead of returning only half like charAt().

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.codePointAt(...). No import is needed because java.lang is always visible.

Syntax

Pass a zero-based UTF-16 char index from 0 to sb.length() - 1.

Java
StringBuilder sb = new StringBuilder("A\\uD83D\\uDE00B");
int first = sb.codePointAt(0);    // 65 ('A')
int emoji = sb.codePointAt(1);    // 128512 (0x1F600)
CallWhat it does
sb.codePointAt(0)Code point at index 0.
sb.codePointAt(i)Reads 1 or 2 UTF-16 units starting at i.
i += Character.charCount(cp)Advances loop index past the full code point.

index still counts UTF-16 char units, not logical characters. If index points straight at the second half of a pair, only that low surrogate value is returned.

Library declarations
Java
// java.lang.StringBuilder
public int codePointAt(int index)

Parameters

ParameterDescription
index (int)Zero-based UTF-16 unit index (0 <= index < length()).

Return Value

Return ValueDescription
intThe Unicode code point value starting at index.

Compare with Similar Methods

Working with code points in a StringBuilder:

MethodHow it differs
codePointBefore(index)Reads the code point ending immediately before index.
charAt(index)Returns a single 16-bit char without merging surrogate pairs.
appendCodePoint(cp)Appends a code point onto the end of a builder.
String.codePointAt(index)Equivalent lookup on an immutable String.

Java Examples

Example 1 — Read Full Code Point vs Low Surrogate Half

Inspect "A\\uD83D\\uDE00B" at every index to see how index 1 merges the pair while index 2 reads only the low surrogate.

Java
public class StringBuilderCodePointAtPair {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("A\uD83D\uDE00B");
        System.out.println("length = " + sb.length());
        System.out.println("index 0: " + sb.codePointAt(0) + " (0x" + Integer.toHexString(sb.codePointAt(0)).toUpperCase() + ")");
        System.out.println("index 1: " + sb.codePointAt(1) + " (0x" + Integer.toHexString(sb.codePointAt(1)).toUpperCase() + ")");
        System.out.println("index 2: " + sb.codePointAt(2) + " (0x" + Integer.toHexString(sb.codePointAt(2)).toUpperCase() + ")");
        System.out.println("index 3: " + sb.codePointAt(3) + " (0x" + Integer.toHexString(sb.codePointAt(3)).toUpperCase() + ")");
    }
}

How It Works

  1. Four UTF-16 slots. 'A' sits at 0, the surrogate pair spans 1 and 2, and 'B' sits at 3.
  2. Merged at index 1. Seeing high surrogate 0xD83D followed by low surrogate 0xDE00 yields 128512 (0x1F600).
  3. Unpaired read at index 2. Starting directly at the low surrogate returns only 56832 (0xDE00).

Example 2 — Format All Code Points From a Builder Into Hex

Append mixed characters with appendCodePoint() and collect each code point's hex code into a second builder.

Java
public class StringBuilderCodePointAtLoop {
    public static void main(String[] args) {
        StringBuilder data = new StringBuilder("Hi");
        data.appendCodePoint(0x1F680);

        StringBuilder hexList = new StringBuilder();
        for (int i = 0; i < data.length(); ) {
            int cp = data.codePointAt(i);
            if (hexList.length() > 0) {
                hexList.append(", ");
            }
            hexList.append("U+").append(Integer.toHexString(cp).toUpperCase());
            i += Character.charCount(cp);
        }
        System.out.println(hexList.toString());
    }
}

How It Works

  1. Append supplementary symbol. appendCodePoint(0x1F680) adds two UTF-16 units after "Hi".
  2. Step by charCount. Adding Character.charCount(cp) advances by 1 for 'H'/'i' and 2 for U+1F680.
  3. Collect into builder. The output buffer prints clean Unicode hex notation without relying on emoji font rendering.

Example 3 — Guard Index Bounds and Catch IndexOutOfBoundsException

Verify that codePointAt(sb.length()) throws when no character starts at the end index.

Java
public class StringBuilderCodePointAtBounds {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("OK");
        int lastCp = sb.codePointAt(sb.length() - 1);
        System.out.println("Last code point = " + lastCp);

        try {
            sb.codePointAt(sb.length());
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Caught out-of-range index");
        }
    }
}

How It Works

  1. Valid final slot. For "OK", index length() - 1 (1) holds 'K' (decimal 75).
  2. End index fails. Position length() (2) has no character after it, triggering IndexOutOfBoundsException.
  3. Check before reading. Always verify index >= 0 && index < sb.length() on user indexes.

Frequently Asked Questions

An int holding the Unicode code point starting at the given UTF-16 index.
Because index 2 points at the low surrogate half of the pair; codePointAt() only merges a high surrogate at index with a low surrogate at index + 1.
Read cp = sb.codePointAt(i), then advance i += Character.charCount(cp) so surrogate pairs advance by 2.
It throws IndexOutOfBoundsException when index is negative or >= sb.length().
Whenever the StringBuilder may contain supplementary characters like emoji or rare scripts above U+FFFF.

Next: codePointBefore()

Return the Unicode code point that ends just before a given UTF-16 index.

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

11 people found this page helpful