Java 字符串性质

Java 字符串本质Java字符串本质上是char序列组成.char是一个采用UTF-16编码表示Unicode代码点的代码单元.

Java 字符串本质

Java字符串本质上是char序列组成.
char是一个采用UTF-16编码表示Unicode代码点的代码单元.大多数的常用Unicode字符使用一个代码元就可以表示,而辅助字符需要一对代码元表示.
具体编码规范见 http://ilnba.iteye.com/admin/blogs/1612414

? length方法将返回采用UTF-16编码表示的给定字符串所需要的代码单元数量.

?

?public class StringTest {

public static void main(String[] args){String greeting="\uD950\uDF21hello";int length = greeting.length(); // code unit count is 7int cpCount = greeting.codePointCount(0, length); //code point count is 6greeting.charAt(0); //  the lead surrogate of the first character greeting.charAt(1); //  the trail surrogate of the first charactergreeting.charAt(2); //  hgreeting.charAt(3); //  egreeting.charAt(4); //  l greeting.charAt(5); //  lgreeting.charAt(6); //  0 System.out.println(greeting.charAt(0));System.out.println(greeting.charAt(1));System.out.println(greeting.charAt(2));System.out.println(greeting.charAt(3));System.out.println(greeting.charAt(4));System.out.println(greeting.charAt(5));System.out.println(greeting.charAt(6)); }}

?

在这里\uD950\uDF21表示一个特殊的UTF-16字符也就是一个UTF-16代码点,由两个代码元组成.