请教各位高手:
java中字符采用Unicode编码,难道中文字符和英文字符的长度不是统一的吗,为什么英文字符占一个字节,中文的占两个字节?这样难道不会引起错误吗?编译器如何知道当前的两个字节到底是两个英文还是一个中文? 比如用以下程序实验:public class TestUnicode {
public static void main(String args[]){
String str1 = "hi";
String str2 = "你好";
byte[] b1 = str.getBytes();
byte[] b2 = str.getBytes();
System.out.println(b1.length);
System.out.println(b2.length);
}
}必然一个是2,一个是4.
这是为什么呢?很困惑
java中字符采用Unicode编码,难道中文字符和英文字符的长度不是统一的吗,为什么英文字符占一个字节,中文的占两个字节?这样难道不会引起错误吗?编译器如何知道当前的两个字节到底是两个英文还是一个中文? 比如用以下程序实验:public class TestUnicode {
public static void main(String args[]){
String str1 = "hi";
String str2 = "你好";
byte[] b1 = str.getBytes();
byte[] b2 = str.getBytes();
System.out.println(b1.length);
System.out.println(b2.length);
}
}必然一个是2,一个是4.
这是为什么呢?很困惑
http://blog.csdn.net/java2000_net/archive/2008/10/10/3047001.aspx