Java- 使用 new 关键字创建 String 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28427483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java- Creating String object using new keyword
提问by Ankit Sharma
I know the difference between String literal and new String object and also know how it works internally.But my question is little bit advance of this.When we create String object using new keyword as
我知道 String 文字和 new String 对象之间的区别,也知道它在内部是如何工作的。但我的问题有点提前。当我们使用 new 关键字创建 String 对象时
String str = new String("test");
In this case, we are passing a argument of String type. My questions is where this string gets generated - Heap Or String constant pool Or somewhere else?
在这种情况下,我们传递的是 String 类型的参数。我的问题是这个字符串是从哪里生成的——堆或字符串常量池或其他地方?
As up to my knowledge, this argument is a string literal so it should be in String constant pool.If is it so then what is use of intern
method - only just link variable str
to constant pool? because "test"
would be available already.
据我所知,这个参数是一个字符串文字,所以它应该在字符串常量池中。如果是这样,那么intern
方法的用途是什么- 只是将变量链接str
到常量池?因为"test"
已经可以使用了。
Please clarify me, if I had misunderstood the concept.
如果我误解了这个概念,请澄清我。
回答by M Anouti
The statement String str = new String("test");
creates a string object which gets stored on the heap like any other object. The string literal "test"
that is passed as an argument is stored in the string constant pool.
该语句String str = new String("test");
创建一个字符串对象,该对象像任何其他对象一样存储在堆中。"test"
作为参数传递的字符串文字存储在字符串常量池中。
String#intern()
checks if a string constant is already available in the string pool. If there is one already it returns it, else it creates a new one and stores it in the pool. See the Javadocs:
String#intern()
检查字符串池中是否已经存在字符串常量。如果已经有一个它返回它,否则它创建一个新的并将它存储在池中。请参阅Javadoc:
Returns a canonical representation for the string object.
A pool of strings, initially empty, is maintained privately by the class
String
.When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the
equals(Object)
method, then the string from the pool is returned. Otherwise, thisString
object is added to the pool and a reference to thisString
object is returned.It follows that for any two strings
s
andt
,s.intern() == t.intern()
is true if and only ifs.equals(t)
is true.
返回字符串对象的规范表示。
字符串池最初是空的,由类私下维护
String
。当调用 intern 方法时,如果池中已经包含一个与该
equals(Object)
方法确定的 String 对象相等的字符串,则返回池中的字符串。否则,将此String
对象添加到池中并String
返回对此对象的引用。由此可知,对于任意两个字符串
s
andt
,s.intern() == t.intern()
当且仅当s.equals(t)
为真时为真。
Starting from JDK7, interned strings are stored on the heap. This is from the release notes of JDK7:
从 JDK7 开始,interned 字符串存储在堆上。这是来自JDK7的发行说明:
In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the
String.intern()
method will see more significant differences.
在 JDK 7 中,interned 字符串不再分配在 Java 堆的永久代中,而是与应用程序创建的其他对象一起分配在 Java 堆的主要部分(称为年轻代和年老代)中. 此更改将导致更多数据驻留在主 Java 堆中,而永久代中的数据更少,因此可能需要调整堆大小。由于此更改,大多数应用程序只会看到相对较小的堆使用差异,但加载许多类或大量使用该
String.intern()
方法的较大应用程序将看到更显着的差异。
回答by TheLostMind
Use of intern()
:
使用intern()
:
public static void main(String[] args) throws IOException {
String s = new String(new char[] { 'a', 'b', 'c' }); // "abc" will not be added to String constants pool.
System.out.println(System.identityHashCode(s));
s = s.intern();// add s to String constants pool
System.out.println(System.identityHashCode(s));
String str1 = new String("hello");
String str2 = "hello";
String str3 = str1.intern();
System.out.println(System.identityHashCode(str1));
System.out.println(System.identityHashCode(str2));
System.out.println(System.identityHashCode(str3));
}
O/P :
开/关:
1414159026
1569228633 --> OOPs String moved to String constants pool
778966024
1021653256
1021653256 --> "hello" already added to string pool. So intern does not add it again.