Underlying mechanism of String pooling in Java?(Java中字符串池的底层机制?)
问题描述
我很好奇为什么不需要调用 那么我们怎样才能使用 这个post 澄清了 看到诸如new String()
就可以创建字符串,因为 API 提到它是 class<的
Object
/code> java.lang.String
String s="hi"
而不是 String s=new String("hi")
?==
运算符的使用和 new
的缺失,并表示这是由于 interned 的 String
文字 或由 JVM
从文字池中获取,因此 Strings
是不可变的.
String s="hi"
第一次真正发生了什么?
JVM
是否像这样替换它String s=new String("hi")
,其中创建了一个对象并将"hi"
添加到 String文字池 等后续调用,如String s1="hi"
从泳池里捞出来的?这就是底层机制的运作方式吗?如果是,那么就是
String s=new String("Test");字符串 s1="测试";
和
一样String s="Test";字符串 s1="测试";
在内存利用率和效率方面?
另外,有什么方法可以访问字符串池检查其中存在多少
String
文字,占用了多少空间等?
String s="hi"
第一次真正发生了什么?JVM 是否像这样替换它
String s=new String("hi")
,其中创建了一个对象并将hi"添加到字符串文字中pool 等后续调用(例如 String s1="hi" )取自游泳池?.没有.真正发生的是 - String Literals 在编译期间被 resolved 并在类被 interned (添加到 String 常量池中)加载/初始化或懒惰地.因此,它们可用于 JVM 中的类.请注意,即使您在字符串常量池中有一个值为
<块引用>"hi"
的字符串,new String("hi")
也会创建堆上的另一个字符串并返回它的引用.
- 是
String s=new String("Test");字符串 s1="测试";
<块引用>
和
一样String s="Test";字符串 s1="测试";
<块引用>
在内存利用率和效率?
不,在第一种情况下,创建了 2 个测试"字符串.一个将被添加到字符串常量池中(假设它尚未存在),另一个将添加到堆中.第二个可以GCed.在第二种情况下,字符串常量池中只有一个String literal,并且有2个对它的引用(
<块引用>s
和s1
).
- 如果有任何方法可以访问字符串池,如检查其中存在多少字符串文字,占用空间等来自程序或任何监控工具?
我认为我们看不到字符串常量池的内容.我们只能假设并确认基于我们的假设的行为.
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中字符串池的底层机制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java中字符串池的底层机制?
基础教程推荐
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01