Java Character toChars() Method

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

Definition

In Java, Character.toChars(codePoint) returns the code point as a char[] in UTF-16: an array of length 1 for BMP code points, and of length 2 (high surrogate, then low surrogate) for supplementary ones.

It is the standard Java 5+ way to turn a code point into text: new String(Character.toChars(cp)). A second overload writes the chars into an array you already have and returns how many it wrote.

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

Syntax

Two overloads: one returns a new array, the other fills an existing one.

Java
char[] a = Character.toChars('A');        // {'A'}
char[] b = Character.toChars(0x1F600);    // {'\uD83D', '\uDE00'}
String s = new String(Character.toChars(0x1F600));   // the emoji as a String

char[] buf = new char[4];
int n = Character.toChars(0x1F600, buf, 1);          // n = 2, writes buf[1] and buf[2]
CallWhat it does
new String(Character.toChars(cp))One code point as a String.
sb.append(Character.toChars(cp))Append a code point to a StringBuilder.
pos += Character.toChars(cp, buf, pos)Write into a buffer and advance the position.

Since Java 11, the static toString(int) overload does the new String(toChars(cp)) step in one call; StringBuilder.appendCodePoint(cp) works on Java 5+.

Library declarations
Java
// java.lang.Character (since Java 5)
public static char[] toChars(int codePoint)
public static int toChars(int codePoint, char[] dst, int dstIndex)

Parameters

ParameterDescription
codePoint (int)A valid code point, 0 to 0x10FFFF.
dst (char[])Second overload: the array to write into.
dstIndex (int)Second overload: where to write the first char.

Return Value

Return ValueDescription
char[]First overload: a new array of length 1 or 2.
intSecond overload: the number of chars written, 1 or 2.

Compare with Similar Methods

Other ways to turn a code point into text:

MethodHow it differs
toString(int)The static code point overload returns a String directly, Java 11+.
sb.appendCodePoint(cp)Appends to a StringBuilder, Java 5+.
highSurrogate(cp)Only one half; unspecified for BMP input (Java 7+).

Java Examples

Example 1 — What toChars Returns for a Letter and an Emoji

Print the length and every char value of the arrays for U+0041 and U+1F600.

Java
public class CharacterToCharsArrays {
    public static void main(String[] args) {
        int[] cps = {'A', 0x1F600};
        for (int cp : cps) {
            char[] units = Character.toChars(cp);
            StringBuilder values = new StringBuilder();
            for (char u : units) {
                values.append(' ').append((int) u);
            }
            System.out.println("U+" + Integer.toHexString(cp) + " -> length " + units.length + ":" + values);
        }
    }
}

How It Works

  1. A BMP letter. 'A' fits in one char, so the array is {65}.
  2. An emoji. U+1F600 needs a surrogate pair, so the array holds 55357 (0xD83D) and then 56832 (0xDE00).
  3. Print values, not the array. The inner loop casts each char to int; concatenating the array itself would print a type code and hash, not its content.

Example 2 — Build Text from a List of Code Points

Append code points with toChars, then check the result against a string literal.

Java
public class CharacterToCharsBuild {
    public static void main(String[] args) {
        int[] cps = {'H', 'i', ' ', 0x1F600};
        StringBuilder sb = new StringBuilder();
        for (int cp : cps) {
            sb.append(Character.toChars(cp));
        }
        String text = sb.toString();
        System.out.println("length=" + text.length());
        System.out.println("matches literal: " + text.equals("Hi \uD83D\uDE00"));
        System.out.println("single emoji String length: " + new String(Character.toChars(0x1F600)).length());
    }
}

How It Works

  1. append(char[]). Each call adds all chars of the returned array, whether there is one or two.
  2. Same as the literal. The built text equals "Hi \uD83D\uDE00": three BMP chars plus the two halves of the emoji, 5 chars.
  3. One code point, one String. new String(Character.toChars(0x1F600)) is the Java 8 way to make a one-character string from any code point.

Example 3 — Write into a Buffer, Then Break the Rules

Use the three-argument overload, then pass a buffer with too little room and an invalid code point.

Java
public class CharacterToCharsErrors {
    public static void main(String[] args) {
        char[] dst = new char[2];
        System.out.println("written: " + Character.toChars('Z', dst, 1) + ", dst[1]=" + dst[1]);
        try {
            Character.toChars(0x1F600, dst, 1);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("no room for a pair: IndexOutOfBoundsException");
        }
        try {
            Character.toChars(-1);
        } catch (IllegalArgumentException e) {
            System.out.println("-1: IllegalArgumentException");
        }
    }
}

How It Works

  1. Return value is a count. Writing 'Z' at index 1 needs one char, so the method returns 1 and dst[1] becomes Z.
  2. Not enough room. The emoji needs two slots starting at index 1, but the array only has indexes 0 and 1, so an IndexOutOfBoundsException is thrown.
  3. Not a code point. -1 is outside 0 to 0x10FFFF, and toChars rejects it with IllegalArgumentException.

Frequently Asked Questions

new String(Character.toChars(cp)). On Java 11 and newer, the static toString(int) overload does the same.
1 for code points up to 0xFFFF, 2 for code points from 0x10000 to 0x10FFFF.
IllegalArgumentException, for any value below 0 or above 0x10FFFF.
The number of chars it wrote into the destination array: 1 or 2.
println(char[]) prints the characters, but "text" + array prints a type code and hash. Convert with new String(...) first.

Next: toCodePoint()

Combine a high and a low surrogate into one supplementary code point.

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