Java Character charCount() method

Java charCount() method is a part of Character class of the java.lang package. This method is used for the determination of the total number of char values required for the representation of a character i.e it counts the total number of char values.

Syntax:

public static int charCount(int codePoint)  

Parameter:

The parameter passed is int codePoint which is the character to be tested for a number of char values.

Returns:

Returns 2 if the value of the parameter is equal to or greater than 0X10000, else returns 1.

Example 1:

Here, different integer checkpoints values are passed and the number of char values required for representation is returned. 1 will be returned for the value less than 0X10000 and 2 will be returned for greater values.

import java.lang.Character;

public class StudyTonight 
{  
    public static void main(String[] args) 
    {  
       int cp1 = 88;  
       int cp2 = 2389067;  
  
       int c1 = Character.charCount(cp1);  
       System.out.println(cp1 + " requires "+ c1 + " values.");  // Check the number of char values required for representation
    
       int c2 = Character.charCount(cp2);  
       System.out.println(cp2 + " requires " + c2 + " values.");  
    }  
} 


88 requires 1 values.
2389067 requires 2 values.

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.util.Scanner; 

public class StudyTonight
{  
   public static void main(String[] args)  
     {  
        Scanner sc = new Scanner(System.in);  
        try
        {
          System.out.println("Enter the number");  
          int a= sc.nextInt();  
          System.out.println("The value required is: "+   +Character.charCount(a));
          
        }
        catch(Exception e)
        {
          System.out.println("Invalid Number!!");
        }
     }  
}


Enter the number
77
The value required is: 1
*******************************
Enter the number
89898
The value required is: 2
*******************************
Enter the number
0x5778
Invalid Number!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.