字符串实习在Java 7+中如何工作? [英] How does string interning work in Java 7+?

查看:93
本文介绍了字符串实习在Java 7+中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我意识到我要问的问题与一个又一次被打死的话题有关,但是,即使在阅读了我能找到的所有答案和文档后,我仍然很友善对字符串实习感到困惑.也许是由于我对JVM缺乏了解;可能是由于Java 7中引入的更改使上述许多答案和文档贬值了.无论哪种方式,我都被困住了,希望有人能帮助我更清楚地理解这个概念...

So, I realize the questions I'm about to ask relate to a topic that has been beaten to death time and time again, however, even after reading all of the answers and documentation I could find, I'm still kind of confused about string interning. Perhaps it's due to my lack of understanding for the JVM; perhaps it's due to the changes introduced in Java 7 depreciating many of the aforementioned answers and documentation. Either way, I'm stuck, and I'm hoping someone can help me understand the concept a bit more clearly...

String a = "text";
String b = new String("text");

在上面的示例中,我知道将创建两个String对象.我也知道在内存中将只有一个包含序列't', 'e', 'x', and 't'的char数组.

In the above example, I understand that two String objects will be created. I also understand that there will be only one char array containing the sequence 't', 'e', 'x', and 't' in memory.

但是,每个字符串对象实际上存储在内存中的什么位置?

However, where in memory are each of the string objects actually stored?

如果我已经阅读了正确的话:变量a的引用对象将存储在常量池中,而b的引用对象将存储在堆中,对吗?

If what I've read I've read correctly: the referent of variable a will be stored in the constant pool whereas the referent of b will be stored in the heap, right?

如果是这种情况,那么我对内部池如何维护内部字符串感到困惑.它是否跟踪常量池中定义的字符串以及从堆中手动插入(调用.intern())的那些字符串? JVM是否创建在常量池中定义的字符串对象并将其加载到内部池中?我对这一切的运作方式感到困惑...

If that be the case, I'm confused as to how the intern pool maintains interned strings. Does it keep track of the Strings defined in the constant pool and those that have been manually interned (invoked .intern()) from the heap? Does the JVM create the string objects defined in the constant pool and load them into the intern pool? I'm confused as to how it all works...

再次,很抱歉问到这些令人困惑/令人反感的问题,仅仅是我对JVM的结构和内部构造还比较陌生,而其中很多使我无所适从.谢谢!

Again, sorry for asking such confusing/asinine questions, it's just that I'm relatively new to the structure and inner-workings of the JVM and a lot of it has left my head spinning. Thanks!

推荐答案

在您声明时,java中有一个叫做String Memory Pool的东西:

There's a thing called String Memory Pool in java, when you declare:

String str1="abc";

它将转到该内存池,而不是在堆上.但是当你写:

It goes to that memory pool and not on the heap. But when you write:

String str2=new String("abc");

如果您再次写:

String str3 = "abc"; 

它将不会在池上创建更多对象,它将检查池中是否存在此文字,并将其分配给该对象.但是写:

It won't create any more object on the pool, it will check the pool if this literal already exists it will assign that to it. But writing:

String str4 = new String("abc");

将再次在堆上创建一个新对象

will again create a new object on the heap

关键是:

一个新对象将总是在您不断编写的时候在堆上创建多次:

A new object will always be created on the heap as many times as you keep writing:

new String("abc");

但是,如果您不使用关键字new继续直接分配字符串,则只会从内存池中引用它(或者如果内存池中不存在它,则创建它)

But if you keep assigning the Strings directly without using the keyword new, it will just get referenced from the memory pool (or get created if not present in the memory pool)

intern()方法查找字符串是否存在于内存池中(如果不是),它将字符串添加到内存池中并返回对其的引用.因此,使用此方法后,您的String引用不会指向堆上的任何对象,而是指向String内存池中的一个对象(还要注意,该内存池仅包含唯一的字符串).

intern() method finds if the string is present in the memory pool if it is not it adds it to the memory pool and returns a reference to it. so after using this method the String reference of yours is not pointing to any object on the heap, it is pointing to an object in the String Memory Pool (Also, note that the memory pool only contains unique strings).

这篇关于字符串实习在Java 7+中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆