先说结论,和c++、c完全不同,不能理解为一个char就是一个byte。
不过java其实也不需要关心到底分配了多少内存,只用String就能避免谈论byte(很少情况直接拿出byte【】来用!)
直接看代码:
public static void main(String[] args) throws IOException {
// Character.MAX_CODE_POINT
String zhongWen = "中中中国国国";
char[] chars = new char[zhongWen.length()];
System.out.println(chars.length);
zhongWen.getChars(0, zhongWen.length(), chars, 0);
for (char aChar : chars) {
System.out.printf("%c", aChar);
}
System.out.println();
byte[] bytes = zhongWen.getBytes("GBK");
byte[] bytes1 = zhongWen.getBytes("UTF-8");
byte[] bytes2 = zhongWen.getBytes("UNICODE");
System.out.println(bytes.length);
for (byte aByte : bytes) {
System.out.printf("[ %x ] &