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 ?
}
}
输出:
bytes size of Chinese:3
bytes size of English:1
在这里,我的JVM设置为UTF-8,从输出结果可以知道,汉字“龙”需要3个字节,而英文字母“a”只需要1个字节。那么我的问题是:
在Java中,char类型占用2个字节。那么在这里,定义char变量cnc='龙'和enc='a'时,cnc只占用了2个字节而不是3个字节吗?而‘a’是否占用了2个字节而不是1个字节?
char
仍然始终是16位。 - David Conrad