Java String codePointCount() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, codePointCount() counts Unicode code points between two indexes. An emoji that uses two UTF-16 units still counts as one code point.

You pass beginIndex and endIndex. Both are required. The end index is exclusive. The result is an int count.

Syntax

Pass the start index and the exclusive end index.

java
int n = text.codePointCount(0, text.length());
CallWhat it does
text.codePointCount(0, text.length())Counts every Unicode code point in the string.

length() counts UTF-16 units. codePointCount counts code points.

Library declarations
java
public int codePointCount(int beginIndex, int endIndex);

Parameters

ParameterDescription
beginIndexRequired. Where the range starts.
endIndexRequired. Where the range ends. This index is exclusive.

Return Value

Return ValueDescription
intHow many Unicode code points are in the range.

Java Examples

Example 1 — Whole-String Count vs length()

For ASCII-only text the two numbers match. With emoji, codePointCount is the fairer “character” count.

java
public class CodePointCountBasic {
    public static void main(String[] args) {
        String text = "Hi😀";

        int units = text.length();
        int points = text.codePointCount(0, text.length());

        System.out.println("length() = " + units);
        System.out.println("codePointCount = " + points);
    }
}

How It Works

  1. Store the text. "Hi😀" has two letters plus one emoji.
  2. Measure storage. length() returns 4 because the emoji uses two UTF-16 units.
  3. Count code points. codePointCount(0, length()) walks the full range.
  4. Get 3. Each of H, i, and 😀 contributes one code point.

Example 2 — Count Only Part of the String

Pass a smaller range to count just a slice—useful with codePointAt when you already know UTF-16 bounds.

java
public class CodePointCountRange {
    public static void main(String[] args) {
        String text = "Hi😀!";

        int letters = text.codePointCount(0, 2);
        int emoji   = text.codePointCount(2, 4);
        int bang    = text.codePointCount(4, 5);

        System.out.println("letters = " + letters);
        System.out.println("emoji   = " + emoji);
        System.out.println("bang    = " + bang);
    }
}

How It Works

  1. Map indexes. H/i at 0–1; emoji at 2–3; '!' at 4. The end index is exclusive.
  2. Count the prefix. codePointCount(0, 2) covers indexes 0 and 1 → two BMP letters → 2.
  3. Count the emoji slice. codePointCount(2, 4) covers indexes 2 and 3 → one surrogate pair → 1.
  4. Count the suffix. codePointCount(4, 5) covers index 4 → a single '!' → 1.

Example 3 — Enforce a Character Limit

Validate user input by code points, not UTF-16 length— so one emoji does not use up two “slots.” To move an index by N characters, see offsetByCodePoints().

java
public class CodePointCountLimit {
    public static boolean withinLimit(String text, int maxChars) {
        if (text == null) {
            return false;
        }
        return text.codePointCount(0, text.length()) <= maxChars;
    }

    public static void main(String[] args) {
        System.out.println(withinLimit("Hi😀", 3));  // true
        System.out.println(withinLimit("Hi😀", 2));  // false
        System.out.println(withinLimit("Java", 4));  // true
    }
}

How It Works

  1. Guard null. A null string fails the check immediately.
  2. Count code points. Full-range codePointCount gives the character total.
  3. Compare to the limit. "Hi😀" is 3 points—allowed when max is 3, rejected when max is 2.
  4. ASCII still works. "Java" has 4 points and 4 units, so both views agree.

Frequently Asked Questions

codePointCount() counts Unicode code points between beginIndex and endIndex.
Both are required. beginIndex starts the range. endIndex ends it and is exclusive.
An int count of code points.
length() counts UTF-16 units. codePointCount() counts Unicode code points.
StringIndexOutOfBoundsException is thrown.

Next: codePoints()

Stream the Unicode code points of a string, emoji included.

codePoints() 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