public class Test{
private String str="我们";
}
将 Test编译之后形成class文件,那么在class文件中"我们"会以一种CONSTANT_UTF8_info表的形式存在,字节序列如下:
1 0 6 230 136 145 228 187 172
1表示常量表的类型,0 6表示有6个字节的长度。后面6个字节是UTF-8编码的“我们”。
当JVM运行的时候会将这些常量池的信息加载进方法区。也就是说在运 行过程中内存存储的"我们"是UTF-8编码的。
import java.io.UnsupportedEncodingException;
public class Test {
private String str = "我们";
public static void main(String[] args) {
byte[] xx = {(byte)0xE6, (byte)0x88, (byte)0x91, (byte)0xE4, (byte)0xBB, (byte)0xAC};
try {
String str = new String(xx, "UTF-8");
System.out.println(str);
} catch(UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}