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]
Call
What 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
Parameter
Description
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 Value
Description
char[]
First overload: a new array of length 1 or 2.
int
Second overload: the number of chars written, 1 or 2.