Java String codePointAt() Method

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
String Methods

What You’ll Learn

The codePointAt() method returns the full Unicode code point at a string index. It goes beyond charAt() by correctly handling emoji and other characters that need two char values in Java’s UTF-16 storage.

01

Unicode Code Point

Full character identity.

02

Returns int

Not char or String.

03

Surrogate Pairs

Handles emoji correctly.

04

Zero-Based Index

Same indexing as charAt().

05

International Text

Beyond ASCII letters.

06

vs charAt()

Pick the right method.

Definition and Usage

In Java, strings are stored internally as UTF-16 code units. Most letters and symbols fit in one char, but supplementary characters (including many emoji) use a surrogate pair of two char values. The codePointAt(int index) method in java.lang.String reads the complete Unicode character that begins at the given index.

If the character at index is a high surrogate and the next code unit is a low surrogate, Java combines them and returns the supplementary code point. Otherwise it returns the char at that index widened to an int.

💡
Beginner Tip

If your text is plain English with no emoji, codePointAt() and charAt() often give the same numeric value (for example, 'J' is 74 either way). Reach for codePointAt() when you care about true Unicode characters, not just 16-bit chunks.

📝 Syntax

The codePointAt() method is declared in the String class:

java
public int codePointAt(int index)

Parameters

  • index — the index of the first UTF-16 code unit of the character. Must be between 0 and length() - 1 inclusive.

Return Value

Returns the Unicode code point at the specified index as an int (range 0x0 to 0x10FFFF).

Exceptions

Throws StringIndexOutOfBoundsException if index is negative or greater than or equal to the string length.

⚡ Quick Reference

StringCallResult
"Hello, Java!"codePointAt(7)74 ('J')
"Hi ๐Ÿ˜€"codePointAt(3)128512 (U+1F600)
"A"codePointAt(0)65 ('A')
"abc"codePointAt(3)StringIndexOutOfBoundsException
128512Integer.toHexString(cp)"1f600"
Basic
str.codePointAt(0)

First code point

Hex view
Integer.toHexString(cp)

Inspect Unicode value

To chars
Character.toChars(cp)

Convert back to char[]

Step size
Character.charCount(cp)

1 or 2 for loop advance

Examples Gallery

These programs run in Java 8+. They progress from a simple ASCII lookup to emoji handling and comparison with charAt().

📚 Getting Started

Retrieve the Unicode code point for a letter in a familiar greeting.

Example 1 — Basic codePointAt() Usage

Get the code point at index 7 in "Hello, Java!"—the letter J, which is Unicode value 74.

java
public class CodePointAtExample {
    public static void main(String[] args) {
        String sampleString = "Hello, Java!";

        int codePoint = sampleString.codePointAt(7);

        System.out.println("Code point at index 7: " + codePoint);
        System.out.println("As character: " + (char) codePoint);
    }
}

How It Works

Index 7 points to 'J'. Because J fits in one UTF-16 code unit, codePointAt(7) returns 74—the same value you would get from casting charAt(7) to int.

Example 2 — Emoji and Supplementary Characters

The grinning-face emoji is one visible symbol but two char positions in Java. codePointAt() returns the full character.

java
public class EmojiCodePoint {
    public static void main(String[] args) {
        String text = "Hi ๐Ÿ˜€";

        System.out.println("String length (char count): " + text.length());
        System.out.println("Code point at index 3: " + text.codePointAt(3));
        System.out.println("Hex: U+" + Integer.toHexString(text.codePointAt(3)).toUpperCase());
    }
}

How It Works

Indexes 0–2 are H, i, and space. The emoji starts at index 3. length() counts UTF-16 units (5), not visible characters (4). codePointAt(3) reads the surrogate pair and returns 128512 (U+1F600).

📈 Practical Patterns

Compare methods, inspect values, and iterate by code point.

Example 3 — codePointAt() vs charAt()

See why charAt() alone is not enough for emoji—it returns only one half of the surrogate pair.

java
public class CompareMethods {
    public static void main(String[] args) {
        String text = "A๐Ÿ˜€B";
        int emojiStart = 1;

        char half = text.charAt(emojiStart);
        int full  = text.codePointAt(emojiStart);

        System.out.println("charAt(1):      " + (int) half);
        System.out.println("codePointAt(1): " + full);
    }
}

How It Works

charAt(1) returns the high surrogate (55357 / 0xD83D)—not a printable character on its own. codePointAt(1) combines the pair and returns the real emoji code point 128512.

Example 4 — Display a Code Point in Hex

Developers often inspect Unicode values in hexadecimal when debugging international text.

java
public class CodePointHex {
    public static void main(String[] args) {
        String word = "Java";

        for (int i = 0; i < word.length(); i++) {
            int cp = word.codePointAt(i);
            System.out.println("Index " + i + ": U+" +
                String.format("%04X", cp) + " ('" + (char) cp + "')");
        }
    }
}

How It Works

Each ASCII letter has a code point equal to its familiar numeric value. String.format("%04X", cp) pads the hex value to four digits for readable Unicode notation.

Example 5 — Loop by Code Point (Not char)

When strings may contain emoji, advance the index by Character.charCount(cp) instead of always adding 1.

java
public class CodePointLoop {
    public static void main(String[] args) {
        String text = "Go๐Ÿ˜€";

        for (int i = 0; i < text.length(); ) {
            int cp = text.codePointAt(i);
            System.out.println("Code point: " + cp);
            i += Character.charCount(cp);
        }
    }
}

How It Works

Character.charCount(cp) returns 1 for BMP characters and 2 for supplementary ones. This prevents landing in the middle of a surrogate pair when iterating—a common bug when only charAt() is used.

🚀 Common Use Cases

  • Emoji and international text — read complete characters regardless of UTF-16 storage.
  • Character validation — check whether a code point is a letter, digit, or symbol using Character utility methods.
  • Custom parsers — walk strings by real character count instead of raw char count.
  • Debugging encoding — print hex code points to trace unexpected symbols in user input.
  • Building on charAt() — upgrade ASCII-only logic when your app must support global audiences.

🧠 How codePointAt() Works

1

You pass an index

Java receives the String and a zero-based index into its UTF-16 array.

Input
2

Bounds are validated

Out-of-range indexes throw StringIndexOutOfBoundsException before any lookup happens.

Validate
3

Surrogate pair check

If the char at index is a high surrogate, Java reads the next unit and merges them into one code point.

Unicode
=

int code point

The full Unicode scalar value, ready for comparison, formatting, or conversion with Character helpers.

📝 Notes

  • The index parameter refers to a UTF-16 code unit position, not a byte offset in the file or network stream.
  • Valid indexes run from 0 to length() - 1. An invalid index throws StringIndexOutOfBoundsException.
  • A Unicode code point identifies one abstract character; many code points have the same visual appearance in different languages.
  • For BMP characters (most everyday text), codePointAt(i) equals (int) str.charAt(i).
  • When looping, use Character.charCount(codePointAt(i)) to skip surrogate pairs correctly.

⚡ Optimization

codePointAt() runs in constant time (O(1)) for a single lookup, with a small extra cost when checking surrogate pairs. For heavy text processing across an entire string, consider codePoints() (Java 8+) or codePointCount() to work at the Unicode level without manual index arithmetic. For simple ASCII-only paths, charAt() remains slightly leaner and perfectly adequate.

Conclusion

The codePointAt() method bridges Java’s UTF-16 storage model and the Unicode characters users actually see on screen. It is the right tool whenever text may include emoji, accented letters, or scripts beyond basic ASCII.

Start with the ASCII examples to learn the syntax, then study the emoji samples to understand why charAt() alone is not always enough. Combine codePointAt() with Character.charCount() for safe iteration across modern international strings.

💡 Best Practices

✅ Do

  • Use codePointAt() when text may contain emoji or non-Latin scripts
  • Advance loop indexes with Character.charCount(cp)
  • Validate indexes before calling the method on user input
  • Display debug values with Integer.toHexString(cp)
  • Learn charAt() first, then upgrade to code points when needed

❌ Don’t

  • Assume length() equals the number of visible characters
  • Cast a surrogate half from charAt() and treat it as a full symbol
  • Increment loop counters by 1 blindly on international strings
  • Confuse code unit index with byte index in encoded files
  • Use codePointAt() when simple ASCII charAt() logic suffices

Key Takeaways

Knowledge Unlocked

Five things to remember about codePointAt()

Use these points when working with Unicode text in Java.

5
Core concepts
🔢 02

Returns int

Code point value.

Return
😀 03

Emoji Safe

Handles surrogate pairs.

Unicode
📈 04

Loop Smart

Use charCount().

Pattern
📝 05

Build on charAt()

Same index rules.

Basics

❓ Frequently Asked Questions

codePointAt(int index) returns the Unicode code point starting at the given index. For ordinary letters it matches the char value; for emoji and other supplementary characters stored as surrogate pairs, it combines the pair into one full code point.
Call it on any String: string.codePointAt(index). It is defined in java.lang.String and takes one zero-based int index, measured in UTF-16 code units like charAt().
It returns an intโ€”the Unicode code point value (0 to 0x10FFFF). Use Integer.toHexString(cp) to inspect the hex form, or Character.toChars(cp) to convert back to char values.
charAt() returns a single 16-bit char. codePointAt() returns the full Unicode code point as an int. For emoji, charAt() may return only half of the symbol (a surrogate), while codePointAt() returns the complete character.
Java throws StringIndexOutOfBoundsException when index is negative or greater than or equal to string.length(). This is the same exception family used by charAt().
Use codePointAt() when you process international text, emoji, or any string where one visible character may occupy two char positions. For simple ASCII-only logic, charAt() is usually enough.

Master Java String Indexing

Pair codePointAt() with charAt() for complete control over characters—from ASCII letters to emoji.

Next: codePointBefore() →

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.

6 people found this page helpful