由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - string pool vs class constant pool问题,在线等
相关主题
Core Java那两本砖头书太浅了JDK Source?
TIJ上写错了?HashMap cache
Java大侠们:Hashtable help please!Re: 怎样不用main(String args[])输出"hello worl
问一个Java题[转载] Java 1.5 Generic 问题
请教一个问题,thanks!Java练习题 12
Java program running on PDA那个快?
Java runtime array memory layout?你们有人测试过这种语法么?
怎么样让jar在64位JRE下运行啊?Java StringBuilder myth debunked
相关话题的讨论汇总
话题: string话题: pool话题: constant话题: class话题: hello
进入Java版参与讨论
1 (共1页)
OE
发帖数: 369
1
想知道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)。
t***a
发帖数: 416
2
如果你看编译好的bytecode, 两处应该都是LDC "Hello!", LDC都是生成一个string
refere
nce, 然后再把这个reference存储给static field或者local variable. 所以如果这么
看结果应该是false
但是jvm对于相同string constant在不同class files这种情况,有个优化,它只
create一个instance, 所以其实两个reference指向了同一个Instance,结果就变成了
true

【在 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
发帖数: 369
3
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.指向它.

【在 t***a 的大作中提到】
: 如果你看编译好的bytecode, 两处应该都是LDC "Hello!", LDC都是生成一个string
: refere
: nce, 然后再把这个reference存储给static field或者local variable. 所以如果这么
: 看结果应该是false
: 但是jvm对于相同string constant在不同class files这种情况,有个优化,它只
: create一个instance, 所以其实两个reference指向了同一个Instance,结果就变成了
: true

t***a
发帖数: 416
4
我就是看了你的帖子,和你程序的结果,自己瞎联想的
constant pool(是叫这个名字么)是per class的,而且是runtime的,但是这个string
的instance肯定是整个jvm来share的,只有一个,那么constant pool里应该只存ref,
而且还是strong ref(它不能让这个instance被gc)
string pool是存soft ref的,它是所有classes共享的,如果所有class里的strong
ref都被回收了,那么string pool里面引用的这个string instance也会被回收的
这么看来。。。的确是不同的。。。。楼主为啥要比较他们两个呢?
t***a
发帖数: 416
5
这东西做面试题不错。。。。。

stores
class
object

【在 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

OE
发帖数: 369
6
原因之一就是很多人管string pool叫string constant pool.

string

【在 t***a 的大作中提到】
: 我就是看了你的帖子,和你程序的结果,自己瞎联想的
: constant pool(是叫这个名字么)是per class的,而且是runtime的,但是这个string
: 的instance肯定是整个jvm来share的,只有一个,那么constant pool里应该只存ref,
: 而且还是strong ref(它不能让这个instance被gc)
: string pool是存soft ref的,它是所有classes共享的,如果所有class里的strong
: ref都被回收了,那么string pool里面引用的这个string instance也会被回收的
: 这么看来。。。的确是不同的。。。。楼主为啥要比较他们两个呢?

OE
发帖数: 369
7
想知道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)。
t***a
发帖数: 416
8
如果你看编译好的bytecode, 两处应该都是LDC "Hello!", LDC都是生成一个string
refere
nce, 然后再把这个reference存储给static field或者local variable. 所以如果这么
看结果应该是false
但是jvm对于相同string constant在不同class files这种情况,有个优化,它只
create一个instance, 所以其实两个reference指向了同一个Instance,结果就变成了
true

【在 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
发帖数: 369
9
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.指向它.

【在 t***a 的大作中提到】
: 如果你看编译好的bytecode, 两处应该都是LDC "Hello!", LDC都是生成一个string
: refere
: nce, 然后再把这个reference存储给static field或者local variable. 所以如果这么
: 看结果应该是false
: 但是jvm对于相同string constant在不同class files这种情况,有个优化,它只
: create一个instance, 所以其实两个reference指向了同一个Instance,结果就变成了
: true

t***a
发帖数: 416
10
我就是看了你的帖子,和你程序的结果,自己瞎联想的
constant pool(是叫这个名字么)是per class的,而且是runtime的,但是这个string
的instance肯定是整个jvm来share的,只有一个,那么constant pool里应该只存ref,
而且还是strong ref(它不能让这个instance被gc)
string pool是存soft ref的,它是所有classes共享的,如果所有class里的strong
ref都被回收了,那么string pool里面引用的这个string instance也会被回收的
这么看来。。。的确是不同的。。。。楼主为啥要比较他们两个呢?
t***a
发帖数: 416
11
这东西做面试题不错。。。。。

stores
class
object

【在 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

OE
发帖数: 369
12
原因之一就是很多人管string pool叫string constant pool.

string

【在 t***a 的大作中提到】
: 我就是看了你的帖子,和你程序的结果,自己瞎联想的
: constant pool(是叫这个名字么)是per class的,而且是runtime的,但是这个string
: 的instance肯定是整个jvm来share的,只有一个,那么constant pool里应该只存ref,
: 而且还是strong ref(它不能让这个instance被gc)
: string pool是存soft ref的,它是所有classes共享的,如果所有class里的strong
: ref都被回收了,那么string pool里面引用的这个string instance也会被回收的
: 这么看来。。。的确是不同的。。。。楼主为啥要比较他们两个呢?

n******1
发帖数: 3756
13
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.
1 (共1页)
进入Java版参与讨论
相关主题
Java StringBuilder myth debunked请教一个问题,thanks!
请教一段代码,关于hashCode()Java program running on PDA
关于==和equalsJava runtime array memory layout?
也问个 HashMap问题怎么样让jar在64位JRE下运行啊?
Core Java那两本砖头书太浅了JDK Source?
TIJ上写错了?HashMap cache
Java大侠们:Hashtable help please!Re: 怎样不用main(String args[])输出"hello worl
问一个Java题[转载] Java 1.5 Generic 问题
相关话题的讨论汇总
话题: string话题: pool话题: constant话题: class话题: hello