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)
Call
What 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
Parameter
Description
index (int)
Zero-based UTF-16 unit index (0 <= index < length()).
length = 4
index 0: 65 (0x41)
index 1: 128512 (0x1F600)
index 2: 56832 (0xDE00)
index 3: 66 (0x42)
How It Works
Four UTF-16 slots.'A' sits at 0, the surrogate pair spans 1 and 2, and 'B' sits at 3.
Merged at index 1. Seeing high surrogate 0xD83D followed by low surrogate 0xDE00 yields 128512 (0x1F600).
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());
}
}
Output
U+48, U+69, U+1F680
How It Works
Append supplementary symbol.appendCodePoint(0x1F680) adds two UTF-16 units after "Hi".
Step by charCount. Adding Character.charCount(cp) advances by 1 for 'H'/'i' and 2 for U+1F680.
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");
}
}
}
Output
Last code point = 75
Caught out-of-range index
How It Works
Valid final slot. For "OK", index length() - 1 (1) holds 'K' (decimal 75).
End index fails. Position length() (2) has no character after it, triggering IndexOutOfBoundsException.
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.