我已经阅读了以下帖子:
现在考虑下面给出的代码:
public static void main(String[] args) {
printCharacterDetails("?");
}
public static void printCharacterDetails(String character){
System.out.println("Unicode Value for "+character+"="+Integer.toHexString(character.codePointAt(0)));
byte[] bytes = character.getBytes();
System.out.println("The UTF-8 Character="+character+" | Default: Number of Bytes="+bytes.length);
String stringUTF16 = new String(bytes, StandardCharsets.UTF_16);
System.out.println("The corresponding UTF-16 Character="+stringUTF16+" | UTF-16: Number of Bytes="+stringUTF16.getBytes().length);
System.out.println("----------------------------------------------------------------------------------------");
}
Run Code Online (Sandbox Code Playgroud)
当我尝试character.getBytes()
在上面的代码中调试行时,调试器将我带入getBytes()
String类的方法,然后进入static byte[] encode(char[] ca, int off, int len)
StringCoding类的方法.String csn = Charset.defaultCharset().name();
在调试过程中,encode method()的第一行返回"UTF-8"作为默认编码.我预计它会是"UTF-16".
该计划的输出是:
最大的Unicode值= 6700 UTF-8字符=最| 默认值:字节数= 3
相应的UTF-16字符= | UTF-16:字节数= 6 …