想知道JVM内部到底是如何实现string pool的。 string pool里存的到底是string objects还是string references? string pool和class constant pool的关系是什么样的? public class hello1{ public static String s="Hello!" } public class hello2{ public static void main (String[] args){ String s = "Hello!"; System.out.println(s==hello1.s);//this is true } } if you check the constant pool in .class files, you can see both classes have "#n = Utf8 Hello!" in their constant pool. 但我猜,当JVM load这两个 classes,它们各自的runtime constant pool把"Hello1"存成了string reference, 这 两个string references都指向了同一个string object. 这个string object可能就在 string pool里(permgen上);也有可能还是在heap里,但是它的一个reference 存在 了string pool里(weak hashmap)。
【在 OE 的大作中提到】 : 想知道JVM内部到底是如何实现string pool的。 : string pool里存的到底是string objects还是string references? : string pool和class constant pool的关系是什么样的? : public class hello1{ : public static String s="Hello!" : } : public class hello2{ : public static void main (String[] args){ : String s = "Hello!"; : System.out.println(s==hello1.s);//this is true
OE
4 楼
ldc是这样解释的: Pushing a String causes a reference to a java.lang.String object to be constructed and pushed onto the operand stack. astore是这样解释的: Pops objectref (a reference to an object or array) off the stack and stores it in local variable 根据这篇文章http://mindprod.com/jgloss/interned.html#UNDERTHEHOOD 我是这样理解的: 那两个string ref.在ldc时就是相同的。与“Hello!”相对应的string object在class loading的时候就生成了,而且会有一个weak string ref.(指向这个string object )被放到一个hashmap里。这个hashmap有时就被称为string pool。当第二个class被 load ,或则说它的runtime constant pool 被生成时,JVM会去hashmap里check “Hello!” ,这时开始的那个string object's ref会被found and return.所以如果用 javap -c -v xxx.class you will see something like ldc #2. this #2 is the 2nd item in the runtime constant pool, which is just the ref. pointing to the only string object. ldc, astore等等都是要等到真正运行时才work。这时,string ref. 已经确定了。 看完这篇文章,我觉得string pool和class constant pool是根本不一样的。但是 class constant pool里的constant_string_table是和生成string pool相关的。 而且 ,在create the 与“Hello!”相对应的string object后,runtime constant pool有 一个指向它的ref., 在hashmap里还有一个weak ref.指向它.
【在 OE 的大作中提到】 : ldc是这样解释的: : Pushing a String causes a reference to a java.lang.String object to be : constructed and pushed onto the operand stack. : astore是这样解释的: : Pops objectref (a reference to an object or array) off the stack and stores : it in local variable : 根据这篇文章http://mindprod.com/jgloss/interned.html#UNDERTHEHOOD : 我是这样理解的: : 那两个string ref.在ldc时就是相同的。与“Hello!”相对应的string object在class : loading的时候就生成了,而且会有一个weak string ref.(指向这个string object
想知道JVM内部到底是如何实现string pool的。 string pool里存的到底是string objects还是string references? string pool和class constant pool的关系是什么样的? public class hello1{ public static String s="Hello!" } public class hello2{ public static void main (String[] args){ String s = "Hello!"; System.out.println(s==hello1.s);//this is true } } if you check the constant pool in .class files, you can see both classes have "#n = Utf8 Hello!" in their constant pool. 但我猜,当JVM load这两个 classes,它们各自的runtime constant pool把"Hello1"存成了string reference, 这 两个string references都指向了同一个string object. 这个string object可能就在 string pool里(permgen上);也有可能还是在heap里,但是它的一个reference 存在 了string pool里(weak hashmap)。
【在 OE 的大作中提到】 : 想知道JVM内部到底是如何实现string pool的。 : string pool里存的到底是string objects还是string references? : string pool和class constant pool的关系是什么样的? : public class hello1{ : public static String s="Hello!" : } : public class hello2{ : public static void main (String[] args){ : String s = "Hello!"; : System.out.println(s==hello1.s);//this is true
OE
10 楼
ldc是这样解释的: Pushing a String causes a reference to a java.lang.String object to be constructed and pushed onto the operand stack. astore是这样解释的: Pops objectref (a reference to an object or array) off the stack and stores it in local variable 根据这篇文章http://mindprod.com/jgloss/interned.html#UNDERTHEHOOD 我是这样理解的: 那两个string ref.在ldc时就是相同的。与“Hello!”相对应的string object在class loading的时候就生成了,而且会有一个weak string ref.(指向这个string object )被放到一个hashmap里。这个hashmap有时就被称为string pool。当第二个class被 load ,或则说它的runtime constant pool 被生成时,JVM会去hashmap里check “Hello!” ,这时开始的那个string object's ref会被found and return.所以如果用 javap -c -v xxx.class you will see something like ldc #2. this #2 is the 2nd item in the runtime constant pool, which is just the ref. pointing to the only string object. ldc, astore等等都是要等到真正运行时才work。这时,string ref. 已经确定了。 看完这篇文章,我觉得string pool和class constant pool是根本不一样的。但是 class constant pool里的constant_string_table是和生成string pool相关的。 而且 ,在create the 与“Hello!”相对应的string object后,runtime constant pool有 一个指向它的ref., 在hashmap里还有一个weak ref.指向它.
【在 OE 的大作中提到】 : ldc是这样解释的: : Pushing a String causes a reference to a java.lang.String object to be : constructed and pushed onto the operand stack. : astore是这样解释的: : Pops objectref (a reference to an object or array) off the stack and stores : it in local variable : 根据这篇文章http://mindprod.com/jgloss/interned.html#UNDERTHEHOOD : 我是这样理解的: : 那两个string ref.在ldc时就是相同的。与“Hello!”相对应的string object在class : loading的时候就生成了,而且会有一个weak string ref.(指向这个string object
http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#d5e Literal strings within the same class (§8) in the same package (§7) represent references to the same String object (§4.3.1). Literal strings within different classes in the same package represent references to the same String object. Literal strings within different classes in different packages likewise represent references to the same String object. Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals. Strings computed by concatenation at run time are newly created and therefore distinct. The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.