import java.io.UnsupportedEncodingException;
public class TestChar {
public static void main(String[] args) throws UnsupportedEncodingException {
String cnStr = "龙";
String enStr = "a";
byte[] cnBytes = cnStr.getBytes("UTF-8");
byte[] enBytes = enStr.getBytes("UTF-8");
System.out.println("bytes size of Chinese:" + cnBytes.length);
System.out.println("bytes size of English:" + enBytes.length);
// in java, char takes two bytes, the question is:
char cnc = '龙'; // will '龙‘ take two or three bytes ?
char enc = 'a'; // will 'a' take one or two bytes ?
}
}
Output :
bytes size of Chinese:3
bytes size of English:1
这里,我的JVM设置为UTF-8,从输出中我们知道汉字“龙”占3个字节,英文字符“a”占1个字节。我的问题是:
在Java中,char占用两个字节,这里,char cnc = '龙';字符编码 = 'a'; cnc 只需要两个字节而不是 3 个字节吗? 'a' 需要两个字节而不是一个字节?
的代码点值龙
是 40857。它适合一个字符的两个字节。
以 UTF-8 进行编码需要 3 个字节,因为并非所有 2 字节序列在 UTF-8 中都有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)