- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Character charCount() Method
The Java Character charCount() method checks whether a Unicode code point is a supplementary character or not. Literally, the method will determine the number of values required to represent a particular character.
A code point is a number that represents a character in a text representing system. In Unicode, the code point is represented in the form of “U+xxxx” where “xxxx” is an assigned number for the character. The code points after U+FFFF or 0x10000 are called supplementary characters.
Note − 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.
Syntax
Following is the syntax for Java 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. The return type is static int for this method.
Example
The following example shows the usage of Java 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 ); } } }
Output
Let us compile and run the above program, this will produce the following result −
It is a valid supplementary character
Example
Ordinary characters that are represented by one char value are not valid supplementary characters.
Let us see another example that takes these ordinary code points as arguments for the charCount() method.
import java.lang.*; public class CharacterDemo { public static void main(String[] args) { int cp = 0x1235; int res = Character.charCount(cp); if ( res == 1 ) { System.out.println("It is not a valid supplementary character"); } else if ( res == 2 ) { System.out.println("It is a valid supplementary character"); } } }
Output
Let us compile and run the given program above, to have the output displayed as follows −
It is not a valid supplementary character
Example
In this example, let us consider an argument that falls in the supplementary character range. We declare and initialize an integer variable with a code point value "0x12347". This variable is passed as an argument to the method when invoked. The return value will be 2 as the given argument has value greater than 0x10000.
package com.tutorialspoint; import java.lang.*; public class CharacterDemo { public static void main(String[] args) { int cp = 0x12347; int result; result = Character.charCount(cp); System.out.println(result); } }
Output
If the compile and run the code above, the output is displayed as follows −
2
Example
If the initialized integer variable does not hold a supplementary code point, the method returns 1 when it is passed as its argument. The program to demonstrate this is seen below −
package com.tutorialspoint; import java.lang.*; public class CharacterDemo { public static void main(String[] args) { int cp = 0x1234; int res; res = Character.charCount(cp); System.out.println(res); } }
Output
1