Java Character charCount() Method

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

Definition

In Java, Character.charCount(codePoint) returns how many char values are needed to store the code point in UTF-16: 1 for code points below 0x10000, and 2 for code points from 0x10000 upward, which are stored as a surrogate pair.

Its most common use is stepping through a string one character at a time: read a code point with codePointAt(i), then move i forward by charCount(cp) so you never land on the second half of a pair.

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

Syntax

Pass an int code point; the result is always 1 or 2.

Java
int a = Character.charCount('A');       // 1
int b = Character.charCount(0xFFFF);    // 1, last code point of the BMP
int c = Character.charCount(0x1F600);   // 2, an emoji
int d = Character.charCount(0x110000);  // 2, even though this is not a valid code point
CallWhat it does
i += Character.charCount(cp);Advance an index past one code point.
Character.charCount(cp) == 2Will this code point need a surrogate pair?
new char[Character.charCount(cp)]Allocate exactly enough room for one code point.

Only one overload exists, charCount(int codePoint), added in Java 5 with the rest of the code point API.

Library declarations
Java
// java.lang.Character (since Java 5)
public static int charCount(int codePoint)

Parameters

ParameterDescription
codePoint (int)The code point to measure.

Return Value

Return ValueDescription
1codePoint is below 0x10000 (this includes negative numbers).
2codePoint is 0x10000 or greater.

Compare with Similar Methods

Related code point helpers:

MethodHow it differs
isSupplementaryCodePoint(cp)True only for valid code points that need 2 chars.
toChars(cp)Builds the 1- or 2-char array for a valid code point.
String.codePointAt(i)Reads the code point you then measure.

Java Examples

Example 1 — How Many chars Does Each Code Point Need?

Measure a letter, a CJK character, the last BMP code point, the first supplementary one, and an emoji.

Java
public class CharacterCharCountTable {
    public static void main(String[] args) {
        int[] cps = {'A', 0x4E2D, 0xFFFF, 0x10000, 0x1F600};
        for (int cp : cps) {
            System.out.println("0x" + Integer.toHexString(cp) + " needs " + Character.charCount(cp));
        }
    }
}

How It Works

  1. The boundary is 0x10000. Everything up to 0xFFFF, including the CJK character U+4E2D, fits in a single char.
  2. Supplementary needs a pair. 0x10000 is the first code point that does not fit, so it and the emoji U+1F600 report 2.
  3. Hex labels. Integer.toHexString prints the numbers in lowercase hex, matching the U+ notation without the padding.

Example 2 — Size a char Array for a List of Code Points

Add up charCount for every code point, allocate one array, then fill it with toChars.

Java
public class CharacterCharCountBuffer {
    public static void main(String[] args) {
        int[] cps = {'H', 0x1F600, 'i', 0x1D11E};
        int total = 0;
        for (int cp : cps) {
            total += Character.charCount(cp);
        }
        char[] buf = new char[total];
        int pos = 0;
        for (int cp : cps) {
            pos += Character.toChars(cp, buf, pos);
        }
        String s = new String(buf);
        System.out.println("chars needed: " + total);
        System.out.println("length=" + s.length() + " codePoints=" + s.codePointCount(0, s.length()));
    }
}

How It Works

  1. Measure first. H and i need 1 char each; the emoji and MUSICAL SYMBOL G CLEF (U+1D11E) need 2 each, so total is 6.
  2. Fill in place. Character.toChars(cp, buf, pos) writes the 1 or 2 chars at pos and returns how many it wrote, which moves pos forward.
  3. Exactly full. The resulting string has 6 chars but only 4 code points, which confirms the size calculation.

Example 3 — charCount Does Not Check Its Argument

Pass two numbers that are not code points at all, and compare with isValidCodePoint.

Java
public class CharacterCharCountNoCheck {
    public static void main(String[] args) {
        int[] values = {0x110000, -5};
        for (int v : values) {
            System.out.println(v + ": charCount=" + Character.charCount(v)
                    + " valid=" + Character.isValidCodePoint(v));
        }
    }
}

How It Works

  1. A simple comparison. charCount only asks "is the value at least 0x10000?", so 0x110000 (1114112) gets 2.
  2. Negative numbers. -5 is below 0x10000, so it gets 1, even though no character has a negative code point.
  3. Validate separately. isValidCodePoint rejects both values; use it before trusting a number read from a file or user input.

Frequently Asked Questions

1 if the code point is below 0x10000, otherwise 2. It tells you how many chars the code point occupies in a String.
No. It returns 2 for any value of 0x10000 or more, including invalid ones such as 0x110000. Use isValidCodePoint to check.
for (int i = 0; i < s.length(); ) { int cp = s.codePointAt(i); /* use cp */ i += Character.charCount(cp); }. On Java 8 you can also use s.codePoints().
No. The only possible results are 1 and 2, because UTF-16 never uses more than two chars per code point.
For valid code points, charCount(cp) == 2 exactly when isSupplementaryCodePoint(cp) is true. They differ for invalid values above 0x10FFFF.

Next: charValue()

Get the primitive char stored inside a Character object.

charValue() 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.

7 people found this page helpful