更新时间:2022-03-31 22:30:08
当JVM加载包含String文字的类
When JVM loads a class containing a String literal
String str = "hello";
它以UTF-8编码从类文件中读取字符串文字,并从中创建一个char数组
it reads string literal from class file in UTF-8 encoding and creates a char array from it
char[] a = {'h', 'e', 'l', 'l', 'o'};
然后使用String(char [])构造函数从此char数组中创建一个String对象
then it creates a String object from this char array using String(char[]) constructor
new String(a)
然后,JVM将String对象放置在String池中,并将对此String对象的引用分配给 str
变量.
then JVM places the String object in String pool and assigns the reference to this String object to str
variable.