Java String codePointAt() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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);
CallWhat 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

ParameterDescription
indexRequired. The UTF-16 index where the code point starts.

Return Value

Return ValueDescription
intThe 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());
    }
}

How It Works

  1. Store one letter. "A" is a single UTF-16 unit at index 0.
  2. Call codePointAt. codePointAt(0) reads the Unicode scalar value.
  3. Get an int. Result is 65 (decimal), which is U+0041.
  4. 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));
    }
}

How It Works

  1. Map the string. 'H' and 'i' use indexes 0–1; the emoji starts at 2.
  2. Read the full code point. codePointAt(2) β†’ U+1F600.
  3. Compare with charAt. charAt(2) is only the high surrogate (0xD83D), not the emoji.
  4. 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);
        }
    }
}

How It Works

  1. Start at index 0. Read each code point from the current UTF-16 offset.
  2. Call codePointAt. At 0 you get 'A'; at 1 you get the full emoji.
  3. Advance correctly. Add Character.charCount(cp) (1 or 2) instead of always i++.
  4. 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.
StringIndexOutOfBoundsException is thrown.

Next: codePointBefore()

Learn how to read the code point that ends just before an index.

codePointBefore() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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