In Java, codePointAt() returns the Unicode code point that starts at a given index. For an emoji, it can return the full code point, not only one UTF-16 unit.
You pass one index. It is required. The result is an int. The index still counts UTF-16 units.
Syntax
Call codePointAt on the string. Start with this line.
java
int cp = text.codePointAt(0);
Call
What it does
text.codePointAt(0)
Returns the Unicode code point that starts at index 0.
Do not always cast the result to char. Values above 65535 need an int.
Library declarations
java
public int codePointAt(int index);
Parameters
Parameter
Description
index
Required. The UTF-16 index where the code point starts.
Return Value
Return Value
Description
int
The Unicode code point at that index.
Java Examples
Example 1 — Read a Letter’s Code Point
For BMP text, codePointAt returns the same numeric value you would get from casting the char.
java
public class CodePointBasic {
public static void main(String[] args) {
String letter = "A";
int cp = letter.codePointAt(0);
System.out.println("codePointAt(0) = " + cp);
System.out.println("hex = 0x" + Integer.toHexString(cp).toUpperCase());
}
}
Output
codePointAt(0) = 65
hex = 0x41
How It Works
Store one letter."A" is a single UTF-16 unit at index 0.
Call codePointAt.codePointAt(0) reads the Unicode scalar value.
Get an int. Result is 65 (decimal), which is U+0041.
Print hex.Integer.toHexString shows the familiar 0x41 form.
Example 2 — Emoji: codePointAt vs charAt
An emoji often needs two char units. codePointAt returns the full symbol; charAt returns only half.
java
public class CodePointEmoji {
public static void main(String[] args) {
String text = "Hiπ";
int cp = text.codePointAt(2);
char half = text.charAt(2);
System.out.println("length() = " + text.length());
System.out.println("codePointAt(2) = 0x" + Integer.toHexString(cp).toUpperCase());
System.out.println("charAt(2) hex = 0x" + Integer.toHexString(half).toUpperCase());
System.out.println("charCount = " + Character.charCount(cp));
}
}
Map the string.'H' and 'i' use indexes 0β1; the emoji starts at 2.
Read the full code point.codePointAt(2) β U+1F600.
Compare with charAt.charAt(2) is only the high surrogate (0xD83D), not the emoji.
Measure width.Character.charCount(cp) is 2, so skip two units to reach the next character.
Example 3 — Loop by Code Point
Advance with Character.charCount so you do not land in the middle of a surrogate pair. For larger jumps, see offsetByCodePoints().
java
public class CodePointLoop {
public static void main(String[] args) {
String text = "AπB";
for (int i = 0; i < text.length(); ) {
int cp = text.codePointAt(i);
System.out.println("index " + i + " β 0x" + Integer.toHexString(cp).toUpperCase());
i += Character.charCount(cp);
}
}
}
Output
index 0 β 0x41
index 1 β 0x1F600
index 3 β 0x42
How It Works
Start at index 0. Read each code point from the current UTF-16 offset.
Call codePointAt. At 0 you get 'A'; at 1 you get the full emoji.
Advance correctly. Add Character.charCount(cp) (1 or 2) instead of always i++.
Skip the low surrogate. After the emoji, i becomes 3βready for 'B'.
Frequently Asked Questions
codePointAt() returns the Unicode code point that starts at a given index.
index is required. It is a UTF-16 index.
An int Unicode code point.
charAt() returns one UTF-16 char. codePointAt() can return a full code point for surrogate pairs.