java.lang.Character toChars(int codePoint)
Description
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.