- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java.lang.Character.toChars() Method
Description
The java.lang.Character.toChars(int codePoint, char[] dst, int dstIndex) converts the specified character (Unicode code point) to its UTF-16 representation.
If the specified code point is a BMP (Basic Multilingual Plane or Plane 0) value, the same value is stored in dst[dstIndex], and 1 is returned.
If the specified code point is a supplementary character, its surrogate values are stored in dst[dstIndex] (high-surrogate) and dst[dstIndex+1] (low-surrogate), and 2 is returned.
Declaration
Following is the declaration for java.lang.Character.toChars() method
public static int toChars(int codePoint, char[] dst, int dstIndex)
Parameters
codePoint − a Unicode code point
dst − an array of char in which the codePoint's UTF-16 value is stored
dstIndex − the start index into the dst array where the converted value is stored
Return Value
This method returns 1 if the code point is a BMP code point, 2 if the code point is a supplementary code point.
Exception
IllegalArgumentException − if the specified codePoint is not a valid Unicode code point
NullPointerException - if the specified dst is null
IllegalArgumentException - if dstIndex is negative or not less than dst.length, or if dst at dstIndex doesn't have enough array element(s) to store the resulting char value(s). (If dstIndex is equal to dst.length-1 and the specified codePoint is a supplementary character, the high-surrogate value is not stored in dst[dstIndex].)
Example
The following example shows the usage of lang.Character.toChars() method.
package com.tutorialspoint; import java.lang.*; public class CharacterDemo { public static void main(String[] args) { // create an int primitive cp and assign value int cp = 0x0036; // create an int primitive res int res; /** * create a char array dst and assign UTF-16 value of cp * to it using toChars method */ char dst[] = Character.toChars(cp); // check if cp is BMP or supplementary code point using toChars res = Character.toChars(cp, dst, 0); String str1 = "It is a BMP code point"; String str2 = "It is a is a supplementary code point"; // print res value if ( res == 1 ) { System.out.println( str1 ); } else if ( res == 2 ) { System.out.println( str2 ); } } }
Let us compile and run the above program, this will produce the following result −
It is a BMP code point