Java Character codePointCount() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Character

Definition

In Java, Character.codePointCount(seq, beginIndex, endIndex) returns how many code points are in the chars from beginIndex up to (but not including) endIndex. A valid surrogate pair counts as one, and every unpaired surrogate counts as one too.

This is the number a person would call "characters", unlike length(), which counts chars. The char array overload takes an offset and a count instead of two indexes.

A static method of java.lang.Character (Java 5+): call it on the class, as in Character.codePointCount(...); you do not need a Character object. No import is needed, because java.lang is always available.

Syntax

For a CharSequence, pass begin and end indexes; for a char[], pass an offset and a count.

Java
String s = "A\uD83D\uDE00B";
int all = Character.codePointCount(s, 0, s.length());   // 3
int head = Character.codePointCount(s, 0, 2);           // 2: 'A' + a lone high surrogate
int pair = Character.codePointCount(s, 1, 3);           // 1: the emoji
int arr = Character.codePointCount(s.toCharArray(), 1, 3); // 2: chars 1..3 = emoji + 'B'
CallWhat it does
Character.codePointCount(s, 0, s.length())Characters in the whole text.
Character.codePointCount(seq, from, to)Characters in a range; to is exclusive.
Character.codePointCount(chars, offset, count)Characters in count chars starting at offset.

Note the different meaning of the third argument: an end index for CharSequence, a count of chars for char[]. String also has its own codePointCount(begin, end).

Library declarations
Java
// java.lang.Character (since Java 5)
public static int codePointCount(CharSequence seq, int beginIndex, int endIndex)
public static int codePointCount(char[] a, int offset, int count)

Parameters

ParameterDescription
seq / aThe text. Must not be null.
beginIndex, endIndexSequence overload: the char range, end exclusive.
offset, countArray overload: the first char and how many chars to examine.

Return Value

Return ValueDescription
intThe number of code points in the range, from 0 up to the number of chars in it.

Compare with Similar Methods

Different ways to "measure" text:

MethodHow it differs
s.length()Counts chars, so each emoji adds 2.
s.codePoints().count()Java 8 stream version; returns a long.
offsetByCodePoints(...)Converts a code point count back into a char index.

Java Examples

Example 1 — Counting Whole Pairs and Half Pairs

Count the code points in four ranges of the string "A, emoji, B".

Java
public class CharacterCodePointCountRanges {
    public static void main(String[] args) {
        String s = "A\uD83D\uDE00B";
        System.out.println("[0,4) -> " + Character.codePointCount(s, 0, 4));
        System.out.println("[0,2) -> " + Character.codePointCount(s, 0, 2));
        System.out.println("[2,4) -> " + Character.codePointCount(s, 2, 4));
        System.out.println("[1,3) -> " + Character.codePointCount(s, 1, 3));
    }
}

How It Works

  1. The whole string. Four chars hold three code points: A, the emoji, and B.
  2. Cutting the pair. [0,2) ends after the high surrogate and [2,4) starts at the low one; each visible half is counted as one code point.
  3. Exactly the pair. [1,3) covers both halves of the emoji, which together count once.

Example 2 — Enforce a Character Limit That Counts Emoji Once

A status message may have at most 5 characters. Compare a chars-based check with a code-point-based one.

Java
public class CharacterCodePointCountLimit {
    public static void main(String[] args) {
        String status = "Hi \uD83D\uDE00\uD83D\uDE00";
        int limit = 5;
        int chars = status.length();
        int characters = Character.codePointCount(status, 0, chars);
        System.out.println("chars=" + chars + " characters=" + characters);
        System.out.println("fits by chars? " + (chars <= limit));
        System.out.println("fits by characters? " + (characters <= limit));
    }
}

How It Works

  1. Two emoji, four chars. "Hi " is 3 chars, and each U+1F600 adds 2, so length() is 7.
  2. What users see. codePointCount counts each emoji once, so the message has 5 characters.
  3. Pick the rule on purpose. A char-based limit rejects this message although it looks five characters long; a code-point limit accepts it.

Example 3 — Offset and Count on a char Array, and a Range That Is Too Long

Count part of a buffer, all of it, and then a range that runs past the end.

Java
public class CharacterCodePointCountArray {
    public static void main(String[] args) {
        char[] buf = {'a', '\uD83D', '\uDE00', 'b', 'c'};
        System.out.println("offset 1, count 3: " + Character.codePointCount(buf, 1, 3));
        System.out.println("offset 0, count 5: " + Character.codePointCount(buf, 0, 5));
        try {
            Character.codePointCount(buf, 3, 5);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("offset 3, count 5: IndexOutOfBoundsException");
        }
    }
}

How It Works

  1. Count, not end. (buf, 1, 3) examines the three chars at indexes 1, 2, and 3: the emoji pair and 'b', which is 2 code points.
  2. The whole buffer. Five chars contain a, the emoji, b, and c: 4 code points.
  3. Too far. offset + count is 8, more than the 5 chars available, so the call throws IndexOutOfBoundsException.

Frequently Asked Questions

Use Character.codePointCount(s, 0, s.length()) or s.codePointCount(0, s.length()). length() counts each emoji as 2.
Each unpaired surrogate counts as one code point, so the method never throws because of malformed text.
For a CharSequence, the arguments are begin and end indexes. For a char array, they are an offset and a count of chars.
No. endIndex is exclusive, like in String.substring.
Not always. Some symbols, such as flags or emoji with skin tones, are made of several code points. Counting those needs java.text.BreakIterator.

Next: codePointOf()

Look up a code point by its Unicode name, such as "GRINNING FACE" (Java 9+).

codePointOf() tutorial →

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.

11 people found this page helpful