First of all, char type:
The char type is used to represent a single character. To figure out the char type, you must understand the Unicode encoding table.
Determines the use of a 16-bit Unicode character set when designing Java. can represent 65,536 characters.
It is a pity that after a while, the inevitable happens. There are more than 65,536 Unicode characters.
(The main reason is the addition of a large number of Chinese, Japanese, Korean, such as ideographic), that is, 16-bit
The char type has not been able to meet the needs of all Unicode characters.
Code point: Refers to the code value that corresponds to a character in a coded table.
In the Unicode standard, code points are written in hexadecimal and prefixed with u+, for example, u+0041 is
The code point for the letter A.
The code points for Unicode can be divided into 17 code levels (plane).
The first level of code becomes the basic multilingual level (Multilingual plane),
Code points from the u+0000 large u+ffff, which contains the classic Unicode code.
The remaining 16 additional levels of code points from u+10000 to U+10FFFF, including some auxiliary characters
(supplementary character).
The UTF-16 encoding represents all Unicode code points using different lengths of encoding. At the basic level of multiple languages,
Each character is represented by a 16-bit, usually called a code unit. and the auxiliary character is a pair of consecutive
Code unit for the encoding.
Code points and code units in Java:
The Java string consists of a char sequence. The character data type is a Unicode code point that is represented by a UTF-16 encoding
Unit of code.
Most common Unicode characters use a single unit of code to represent
Auxiliary characters require a pair of code unit representations.
The length method returns the number of code units required for the given string represented by the UTF-16 encoding.
String greeting = "Hello";
int n = greeting.length (); is 5
To get the actual length, that is, the number of code points, you can call:
int cpcount = Greeting.codepointcount (0, Greeting.length ());
Excerpt from Java Core Technology Volume I