java.lang.Character toChars(int codePoint)

Description

The Character.toChars(int codePoint) java method converts the specified character (Unicode code point) to its UTF-16 representation stored in a char array. If the specified code point is a BMP (Basic Multilingual Plane or Plane 0) value, the resulting char array has the same value as codePoint. If the specified code point is a supplementary code point, the resulting char array has the corresponding surrogate pair.

The toChars(int codePoint) method of Character class is static thus it should be accessed statically which means the we would be calling this method in this format:

Character.toChars(int codePoint)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java toChars() method non statically.

Method Syntax

public static char[] toChars(int codePoint)

Method Argument

Data Type Parameter Description
int codepoint a Unicode code point

Method Returns

The toChars(int codePoint) method of Character class returns a char array having codePoint’s UTF-16 representation.

Compatibility

Requires Java 1.5 and up

Java Character toChars(int codePoint) Example

Below is a simple java example on the usage of toChars(int codePoint) method of Character class.

package com.javatutorialhq.java.examples;

/*
 * This example source code demonstrates the use of 
 * toChars(int codePoint) method of Character class.
 */

public class CharacterToCharsExample {

	public static void main(String[] args) {
		
		// initialize codepoints
		int codepoint1 = 101;
		int codepoint2 = 98;
		
		// get the UTF-16 representation 
		// of the specified character
		char[] result1 = Character.toChars(codepoint1);
		char[] result2 = Character.toChars(codepoint2);
		String str1="";
		String str2="";
		for(char c:result1){
			str1=str1+c;
		}
		for(char c:result2){
			str2=str2+c;
		}
		
		// print the result
		System.out.println("The UTF-16 representation of "+codepoint1 +
				" is "+str1);
		System.out.println("The UTF-16 representation of "+codepoint2 +
				" is "+str2);
		
	}

}

Sample Output

Below is the sample output when you run the above example.

java Character toChars(int codePoint) example output