Java.lang.Character.charCount() Method

Advertisements


Description

The java.lang.Character.charCount(int codePoint) determines the number of char values needed to represent the specified character (Unicode code point). If the specified character is equal to or greater than 0x10000, then the method returns 2. Otherwise, the method returns 1.

This method doesn't validate the specified character to be a valid Unicode code point. The caller must validate the character value using isValidCodePoint if necessary.

Declaration

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

public static int charCount(int codePoint)

Parameters

  • codePoint - the character (Unicode code point) to be tested

Return Value

This method returns 2 if the character is a valid supplementary character, 1 otherwise.

Exception

  • NA

Example

The following example shows the usage of lang.Character.charCount() method.

package com.tutorialspoint;

import java.lang.*;

public class CharacterDemo {

   public static void main(String[] args) {

      // create and assign values to int codepoint cp
      int cp = 0x12345;

      // create an int res
      int res;

      // assign the result of charCount on cp to res
      res = Character.charCount(cp);

      String str1 = "It is not a valid supplementary character";
      String str2 = "It is a valid supplementary character";

      // 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 valid supplementary character



Advertisements
Advertisements