Java Character toChars() Method

The toChars(int codePoint) method of Character class generally converts the specified character into its UTF-16 representation which is usually stored in a char array. If the specified codePoint is a BMP value, the resulting char value array has the same value as the codePoint. On the other hand, if the specified codePoint is a supplementary codePoint, then the resulting char array contains the corresponding surrogate pair.

Syntax

Parameter

codePoint: It is the codePoint which is the character that needs to be tested.

Return Value

The toChars(int codePoint)method returns the char array which has the UTF-16 representation of the character.

Example 1

Output:

The char array with the UTF-16 representation is given as: n

Example 2

Output:

The char array with the UTF-16 representation is given as: o

Java Character toChars(int codePoint, char[] dst, int dstIndex)Method

The toChars(int codePoint, char[] dst, int dstIndex) method of Character class converts the specified character into their respective UTF-16 representation.

If the specified codePoint is BMP(Basic Multilingual Plane) codePoint value, the obtained value is stored in the dstIndex, and 1 is returned. On the other hand, if the specified codePoint is a supplementary character, the obtained surrogate value is stored in the dstIndex, and 1 is returned.

Syntax

Parameter

The above method requires three parameters:

a.)The codePoint which is the character that needs to be tested.

b.)The dst which is an array of character in which the UTF-16 value of the codePoint is stored.

c.)The dstIndex is generally the start index of the dst array in which the converted value is stored.

Returns

The toChars(int codePoint, char[] dst, int dstIndex)method returns 1 if the desired codePoint is a BMP codePoint. Otherwise, the method returns 2 if the desired codePoint is a supplementary codePoint.

Example 1

Output:

The primitive is a BMP code point.
The primitive is a BMP code point.

Example 2

Output:

The primitive is a BMP code point.