Java.lang.Character.toChars() Method

Advertisements


Description

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

Declaration

Following is the declaration for java.lang.Character.toChars() method

public static char[] toChars(int codePoint)

Parameters

  • codePoint - a Unicode code point

Return Value

This method returns a char array having codePoint's UTF-16 representation.

Exception

  • IllegalArgumentException - if the specified codePoint is not a valid Unicode code point.

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 a char array ch
      char ch[];

      // create an int primitive cp and assign value
      int cp = 0x006e;

      // assign result of toChars on cp to ch
      ch = Character.toChars(cp);

      String str = "Char array having cp's UTF-16 representation is ";

      System.out.print( str );

      // use a for loop to print ch
      for (int i=0; i < ch.length; i++){
         System.out.print( ch[i] );
      }
   }
}

Let us compile and run the above program, this will produce the following result:

Char array having cp's UTF-16 representation is n



Advertisements
Advertisements