Java中字符串池的底层机制?

Underlying mechanism of String pooling in Java?(Java中字符串池的底层机制?)

本文介绍了Java中字符串池的底层机制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇为什么不需要调用 new String() 就可以创建字符串,因为 API 提到它是 class<的 Object/code> java.lang.String

那么我们怎样才能使用 String s="hi" 而不是 String s=new String("hi")?

这个post 澄清了 == 运算符的使用和 new 的缺失,并表示这是由于 interned 的 String 文字 或由 JVM 从文字池中获取,因此 Strings 是不可变的.

看到诸如

这样的陈述

String s="hi"

第一次真正发生了什么?

  1. JVM 是否像这样替换它 String s=new String("hi") ,其中创建了一个对象并将 "hi" 添加到 String文字池 等后续调用,如 String s1="hi"从泳池里捞出来的?

  2. 这就是底层机制的运作方式吗?如果是,那么就是

    String s=new String("Test");字符串 s1="测试";

    一样

    String s="Test";字符串 s1="测试";

    在内存利用率和效率方面?

  3. 另外,有什么方法可以访问字符串池检查其中存在多少 String 文字,占用了多少空间等?

解决方案

  1. 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个对它的引用(ss1).

<块引用>

  1. 如果有任何方法可以访问字符串池,如检查其中存在多少字符串文字,占用空间等来自程序或任何监控工具?

我认为我们看不到字符串常量池的内容.我们只能假设确认基于我们的假设的行为.

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 ?

  1. 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?

  2. 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?

  3. 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.?

解决方案

  1. 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.

  1. 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).

  1. 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中字符串池的底层机制?

基础教程推荐