Skip to content
Related Articles

Related Articles

Java Character charCount() with Examples
  • Last Updated : 06 Dec, 2018

The java.lang.Character.charCount() is an inbuilt function in java which is used to determine number of characters needed to represent the specified character. If the character is equal to or greater than 0x10000, the method returns 2 otherwise 1.

Syntax:

public static int charCount(int code)

Parameters: The function accepts a single parameter code. It represents the tested character.

Return Value: It returns 2 if the character is valid otherwise 1.

Errors and Exceptions:



  • This method doesn’t validate the specified character to be a valid Unicode code point.
  • Non-static method is can be called by declaring method_name(argv) in this such case method is static, it should be called by adding the class name as the suffix. We will be get a compilation problem if we call charCount method non-statically.

Examples:

Input : 0x12456
Output : 2
Explanation: the code is greater than 0x10000

Input : 0x9456
Output : 1
Explanation: The code is smaller then 0x10000

Below Programs illustrates the java.lang.Character.charCount() function:
Program 1:




// Java program that demonstrates the use of
// Character.charCount() function
  
// include lang package
import java.lang.*;
  
class GFG {
    public static void main(String[] args)
    {
        int code = 0x9000;
  
        int ans = Character.charCount(code);
  
        // prints 2 if character is greater than 0x10000
        // otherwise 1
        System.out.println(ans);
    }
}


Output:

1

Program 2:




// Java program that demonstrates the use of
// Character.charCount() function
  
// include lang package
import java.lang.*;
  
class GFG {
    public static void main(String[] args)
    {
        int code = 0x12456;
  
        int ans = Character.charCount(code);
  
        // prints 2 if character is greater than 0x10000
        // otherwise 1
        System.out.println(ans);
    }
}


Output:

2

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :