Java中字符串池的基础机制? [英] Underlying mechanism of String pooling in Java?
问题描述
我很好奇为什么可以在没有调用 new String()
的情况下创建字符串,因为API提到它是 Object
of class
java.lang.String
那么我们如何能够使用 String s =hi
而不是 String s = new String(hi)
?
这篇文章澄清了 ==
运算符的使用以及缺少 new
并说这是由字符串
文字实习或<$>从文字池中获取 JVM
,因此字符串
是不可变的。
看到一个声明例如
String s =hi
第一次真正发生了什么?
-
JVM
是否替换它String s = new S tring(hi)
,
其中创建了一个Object并且hi
被添加到 String
文字池以及随后的调用,例如字符串s1 =hi
是从泳池中取出的
吗? -
这是底层机制的运作方式吗?如果是,那么
String s = new String(Test);
String s1 =Test;
与
相同
String s =Test;
String s1 =Test;
内存利用率和效率?
-
此外,有什么方法可以访问字符串池到
检查有多少字符串
文字是存在于其中,占用了多少空间等?
String s =hi
第一次真正发生了什么?
JVM是否像这样替换
String s = new String(hi)
,
其中创建了一个Object,并且hi被添加到String literal
池中,因此后续调用如String s1 =hi取自
池?。
不。真正发生的是 - String Literals 在编译期间已解析并且 interned (添加到String常量池) 加载/初始化或懒洋洋地。因此,它们可供JVM中的类使用。
请注意,即使在字符串常量池中有一个值hi
的字符串, new String(hi)
将创建堆上的另一个String并返回其引用。
- 是
String s = new String(Test);
String s1 =Test;
与
相同
String s =Test;
String s1 =Test;
内存利用率和
效率?
不,在第一种情况下,创建2个测试字符串。一个将被添加到String常量池(假设它不存在)和另一个在堆上。第二种情况可以是GCed。在第二种情况下,String常量池中只有一个String literal ,并且有2个引用( s
和 s1
)。
- 另外,如果我们可以通过哪种方式访问字符串池,就像在
中一样,检查它中存在多少字符串文字,空间占用等等
来自程序或任何监控工具?
我认为我们不能看到String常量池的内容。我们只能基于我们的假设假设和确认行为。
I was curious as to why Strings can be created without a call to new String()
, as the API mentions it is an Object
of class
java.lang.String
So how are we able to use String s="hi"
rather than String s=new String("hi")
?
This post clarified the use of ==
operator and absence of new
and says this is due to String
literals being interned or taken from a literal pool by the JVM
, hence Strings
are immutable.
On seeing a statement such as
String s="hi"
for the first time what really takes place ?
Does the
JVM
replace it like thisString s=new String("hi")
, wherein an Object is created and"hi"
is added to the String literal pool and so subsequent calls such asString s1="hi"
are taken from the pool?Is this how the underlying mechanism operates? If so, then is
String s=new String("Test"); String s1="Test";
the same as
String s="Test"; String s1="Test";
in terms of memory utilization and efficiency?
Also, is there any way by which we can access the String Pool to check how many
String
literals are present in it, how much space is occupied, etc.?
String s="hi"
for the first time what really takes place ?Does the JVM replace it like this
String s=new String("hi")
, wherein an Object is created and "hi" is added to the String literal pool and so subsequent calls such as String s1="hi" are taken from the pool ?.
No. What really happens is - the String Literals are resolved during compile time and interned (added to the String constants pool) as soon as the class is loaded / initialized or lazily. Thus, they are made available to the classes within the JVM.
Note that, even if you have a String with value "hi"
in the Strings constants pool, new String("hi")
will create another String on the heap and return its reference.
- is
String s=new String("Test");
String s1="Test";
the same as
String s="Test";
String s1="Test";
in terms of memory utilization and efficiency?
No, in the first case 2 "Test" Strings are created. One will be added to the String constants pool (assuming it is not already present there) and another on the heap. The second one can be GCed.In the second case, only one String literal is present in the String constants pool and there are 2 references to it (s
and s1
).
- Also if there any way by which we can access the String Pool as in check how many String literals are present in it, space occupied etc from the program or from any monitoring tool?
I don't think we can see the contents of the String constants pool. We can merely assume and confirm the behavior based on our assumptions.
这篇关于Java中字符串池的基础机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!