我们什么时候应该对字符串文字使用 String 的 intern 方法
根据 String#intern(), intern
方法如果在字符串池中找到了该字符串,则返回字符串池中的字符串,否则会在字符串池中添加一个新的字符串对象,并返回该字符串的引用。
所以我尝试了这个:
String s1 = "Rakesh";
String s2 = "Rakesh";
String s3 = "Rakesh".intern();
if ( s1 == s2 ){
System.out.println("s1 and s2 are same"); // 1.
}
if ( s1 == s3 ){
System.out.println("s1 and s3 are same" ); // 2.
}
我期望在 s3 被实习时打印 s1 and s3 are same
,并且 s1 and s2 are same
将不会被打印。但结果是:两行都被打印出来。这意味着,默认情况下,字符串常量是被保留的。但如果是这样,那为什么我们需要 intern
方法呢?换句话说,我们什么时候应该使用这个方法呢?
According to String#intern(), intern
method is supposed to return the String from the String pool if the String is found in String pool, otherwise a new string object will be added in String pool and the reference of this String is returned.
So i tried this:
String s1 = "Rakesh";
String s2 = "Rakesh";
String s3 = "Rakesh".intern();
if ( s1 == s2 ){
System.out.println("s1 and s2 are same"); // 1.
}
if ( s1 == s3 ){
System.out.println("s1 and s3 are same" ); // 2.
}
I was expecting that s1 and s3 are same
will be printed as s3 is interned, and s1 and s2 are same
will not be printed. But the result is: both lines are printed. So that means, by default String constants are interned. But if it is so, then why do we need the intern
method? In other words when should we use this method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
Java 自动实习字符串文字。这意味着在许多情况下,== 运算符对字符串的作用与对整数或其他原始值的作用相同。
由于字符串文字的实习是自动的,因此
intern()
方法将用于使用new String()
构造的字符串使用您的示例:
将返回:
在所有情况下除了
s4
变量(其值是使用new
运算符显式创建的,并且在其结果上未使用intern
方法)之外,它是一个返回的单个不可变实例 JVM 的字符串常量池。有关详细信息,请参阅 JavaTechniques“字符串相等和实习”。
Java automatically interns String literals. This means that in many cases, the == operator appears to work for Strings in the same way that it does for ints or other primitive values.
Since interning is automatic for String literals, the
intern()
method is to be used on Strings constructed withnew String()
Using your example:
will return:
In all the cases besides of
s4
variable, a value for which was explicitly created usingnew
operator and whereintern
method was not used on it's result, it is a single immutable instance that's being returned JVM's string constant pool.Refer to JavaTechniques "String Equality and Interning" for more information.
在最近的一个项目中,一些巨大的数据结构是用从数据库读取的数据(因此不是字符串常量/文字)设置的,但有大量的重复。这是一个银行应用程序,到处都出现了诸如一些规模不大(可能有 100 或 200 家)公司的名称之类的东西。数据结构已经很大,如果所有这些公司名称都是唯一的对象,它们就会溢出内存。相反,所有数据结构都引用相同的 100 或 200 个 String 对象,从而节省了大量空间。
驻留字符串的另一个小优点是,如果保证所有涉及的字符串都被驻留,则可以使用
==
(成功!)来比较字符串。除了更精简的语法之外,这也是性能的增强。 但是正如其他人所指出的,这样做存在引入编程错误的巨大风险,因此这只能作为最后的手段。缺点是,内部字符串比简单地把它扔到堆上需要更多的时间,并且内部字符串的空间可能是有限的,具体取决于 Java 实现。当您处理已知合理数量且具有许多重复项的字符串时,最好这样做。
On a recent project, some huge data structures were set up with data that was read in from a database (and hence not String constants/literals) but with a huge amount of duplication. It was a banking application, and things like the names of a modest set (maybe 100 or 200) corporations appeared all over the place. The data structures were already large, and if all those corp names had been unique objects they would have overflowed memory. Instead, all the data structures had references to the same 100 or 200 String objects, thus saving lots of space.
Another small advantage of interned Strings is that
==
can be used (successfully!) to compare Strings if all involved strings are guaranteed to be interned. Apart from the leaner syntax, this is also a performance enhancement. But as others have pointed out, doing this harbors a great risk of introducing programming errors, so this should be done only as a desparate measure of last resort.The downside is that interning a String takes more time than simply throwing it on the heap, and that the space for interned Strings may be limited, depending on the Java implementation. It's best done when you're dealing with a known reasonable number of Strings with many duplications.
更喜欢
String.equals
而不是this==object
我想在使用
==
和 interned 字符串时添加我的 2 美分。String.equals
做的第一件事是this==object
。请参阅 OpenJDK 上的源代码。因此,尽管有一些微小的性能提升(您没有调用方法),但从维护者的角度来看,使用
==
是一场噩梦,因为一些 interned 字符串有变成非 interned 的趋势。因此,我建议不要依赖
==
的特殊情况来存储字符串,而应始终按照 Gosling 的意图使用equals
。编辑:实习生变成非实习生:
在 2.0 版本中,维护者决定将
hasReferenceVal
公开,但没有详细说明它需要实习字符串数组。现在你有一个错误,可能很难找到,因为在大多数情况下数组包含文字值,有时使用非文字字符串。如果使用
equals
而不是==
,则hasReferenceVal
仍将继续工作。再次强调,性能提升微乎其微,但维护成本却很高。Prefer
String.equals
overthis==object
I want to add my 2 cents on using
==
with interned strings.The first thing
String.equals
does isthis==object
. See source code on OpenJDK.So although there is some miniscule performance gain ( you are not calling a method), from the maintainer point of view using
==
is a nightmare, because some interned strings have a tendency to become non-interned.So I suggest not to rely on special case of
==
for interned strings, but always useequals
as Gosling intended.EDIT: interned becoming non-interned:
In version 2.0 maintainer decided to make
hasReferenceVal
public, without going into much detail that it expects an array of interned strings.Now you have a bug, that may be very hard to find, because in majority of cases array contains literal values, and sometimes a non-literal string is used. If
equals
were used instead of==
thenhasReferenceVal
would have still continue to work. Once again, performance gain is miniscule, but maintenance cost is high.学习 Java String Intern - 一劳永逸
Java 中的字符串在设计上是不可变的对象。因此,即使两个字符串对象具有相同的值,默认情况下也会是不同的对象。然而,如果我们希望节省内存,我们可以通过称为字符串实习生的概念来指示使用相同的内存。
以下规则将帮助您清楚地理解这个概念:
示例:
注意:这里不讨论字符串实习生的动机案例。然而,节省内存肯定是主要目标之一。
Learn Java String Intern - once for all
Strings in java are immutable objects by design. Therefore, two string objects even with same value will be different objects by default. However, if we wish to save memory, we could indicate to use same memory by a concept called string intern.
The below rules would help you understand the concept in clear terms:
Example:
Note: The motivational cases for string intern are not discussed here. However, saving of memory will definitely be one of the primary objectives.
默认情况下,字符串文字和常量会被保留。
也就是说,
"foo" == "foo"
(由字符串文字声明),但是new String("foo") != new String("foo")
。String literals and constants are interned by default.
That is,
"foo" == "foo"
(declared by the String literals), butnew String("foo") != new String("foo")
.你应该弄清楚两个时间段,分别是编译时和运行时。例如:
一方面,在示例1中,我们发现结果都返回true,因为在编译时,jvm会放入“test” "到文字字符串池中,如果jvm发现“test”存在,那么它将使用存在的字符串,在示例1中,“test”字符串都指向相同的内存地址,因此示例1将返回true 。
另一方面,在示例2中,substring()方法在运行时执行,
在 "test" == "!test".substring(1) 的情况下,池将创建两个字符串对象,"test"
和"!test",所以它们是不同的引用对象,所以这种情况会返回false,在"test" == "!test".substring(1).intern()的情况下,intern()方法会将 ""!test".substring(1)" 放入文字字符串池中,因此在这种情况下,它们是相同的引用对象,因此将返回 true。
you should make out two period time which are compile time and runtime time.for example:
in the one hand,in the example 1,we find the results are all return true,because in the compile time,the jvm will put the "test" to the pool of literal strings,if the jvm find "test" exists,then it will use the exists one,in example 1,the "test" strings are all point to the same memory address,so the example 1 will return true.
in the other hand,in the example 2,the method of substring() execute in the runtime time,
in the case of "test" == "!test".substring(1),the pool will create two string object,"test"
and "!test",so they are different reference objects,so this case will return false,in the case of "test" == "!test".substring(1).intern(),the method of intern() will put the ""!test".substring(1)" to the pool of literal strings,so in this case,they are same reference objects,so will return true.
http://en.wikipedia.org/wiki/String_interning
字符串驻留是一种仅存储的方法每个不同字符串值的一份副本,该值必须是不可变的。驻留字符串使某些字符串处理任务更加节省时间或空间,但代价是在创建或驻留字符串时需要更多时间。不同的值存储在字符串内部池中。
http://en.wikipedia.org/wiki/String_interning
string interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned. The distinct values are stored in a string intern pool.
内部字符串避免重复的字符串。实习可以节省 RAM,但代价是需要更多的 CPU 时间来检测和替换重复的字符串。无论有多少引用指向它,每个已被保留的字符串都只有一份副本。由于字符串是不可变的,如果两个不同的方法偶然使用同一个字符串,它们可以共享同一个字符串的副本。将重复字符串转换为共享字符串的过程称为 interning.String.intern(),它为您提供规范主字符串的地址。您可以使用简单的 == (比较指针)来比较 interned 字符串,而不是使用 equals 来一一比较字符串的字符。由于字符串是不可变的,因此实习进程可以自由地进一步节省空间,例如,当“pot”作为其他文字(例如“hippopotamus”)的子字符串存在时,不为“pot”创建单独的字符串文字。
要查看更多信息http://mindprod.com/jgloss/interned.html
Interned Strings avoid duplicate Strings. Interning saves RAM at the expense of more CPU time to detect and replace duplicate Strings. There is only one copy of each String that has been interned, no matter how many references point to it. Since Strings are immutable, if two different methods incidentally use the same String, they can share a copy of the same String. The process of converting duplicated Strings to shared ones is called interning.String.intern() gives you the address of the canonical master String. You can compare interned Strings with simple == (which compares pointers) instead of equals which compares the characters of the String one by one. Because Strings are immutable, the intern process is free to further save space, for example, by not creating a separate String literal for "pot" when it exists as a substring of some other literal such as "hippopotamus".
To see more http://mindprod.com/jgloss/interned.html
输出
OUTPUT
当独立创建两个字符串时,
intern()
允许您比较它们,并且如果引用之前不存在,它还可以帮助您在字符串池中创建引用。当您使用
String s = new String(hi)
时,java会创建该字符串的一个新实例,但是当您使用String s = "hi"
时,java会检查是否存在是否是代码中单词“hi”的实例,如果存在,则仅返回引用。由于比较字符串是基于引用的,因此
intern()
可以帮助您创建引用并允许您比较字符串的内容。当您在代码中使用
intern()
时,它会清除引用同一对象的字符串所使用的空间,并仅返回内存中已存在的同一对象的引用。但如果是 p5,当您使用时:
仅复制 p3 的内容,并新创建 p5。所以它没有被拘留。
所以输出将是:
When two strings are created independently,
intern()
allows you to compare them and also it helps you in creating a reference in the string pool if the reference didn't exist before.When you use
String s = new String(hi)
, java creates a new instance of the string, but when you useString s = "hi"
, java checks if there is an instance of word "hi" in the code or not and if it exists, it just returns the reference.Since comparing strings is based on reference,
intern()
helps in you creating a reference and allows you to compare the contents of the strings.When you use
intern()
in the code, it clears of the space used by the string referring to the same object and just returns the reference of the already existing same object in memory.But in case of p5 when you are using:
Only contents of p3 are copied and p5 is created newly. So it is not interned.
So the output will be:
string intern() 方法用于在字符串常量池中创建堆字符串对象的精确副本。字符串常量池中的字符串对象会自动实习,但堆中的字符串对象则不会。创建实习生的主要用途是节省内存空间并更快地执行字符串对象的比较。
来源: java 中的 string intern 是什么?
string intern() method is used to create an exact copy of heap string object in string constant pool. The string objects in the string constant pool are automatically interned but string objects in heap are not. The main use of creating interns is to save the memory space and to perform faster comparison of string objects.
Source : What is string intern in java?
正如你所说,字符串
intern()
方法首先会从字符串池中查找,如果找到,那么它将返回指向该字符串的对象,或者将一个新的字符串添加到池中。s1
和s2
是两个指向字符串池“Hello”的对象,使用"Hello".intern()
会发现<代码>s1和s2
。因此,"s1 == s3"
返回 true,s3.intern()
也返回 true。As you said, that string
intern()
method will first find from the String pool, if it finds, then it will return the object that points to that, or will add a new String into the pool.The
s1
ands2
are two objects pointing to the String pool "Hello", and using"Hello".intern()
will find thats1
ands2
. So"s1 == s3"
returns true, as well as to thes3.intern()
.通过堆对象引用,如果我们想得到对应的字符串常量池对象引用,那么我们应该使用intern()
Pictorial View
第 1 步:
在堆和字符串常量池中创建带有数据“Rakesh”的对象。另外 s1 始终指向堆对象。
第 2 步:
通过使用堆对象引用 s1,我们尝试使用 intern() 获取相应的字符串常量池对象引用 s2
第 3 步:
有意在字符串常量池中创建一个包含数据“Rakesh”的对象,通过名称 s3 引用
作为“==”运算符,用于引用比较。
s1==s2 得到false
s2==s3 得到true
希望这有帮助!
By using heap object reference if we want to get corresponding string constant pool object reference, then we should go for intern()
Pictorial View
Step 1:
Object with data 'Rakesh' get created in heap and string constant pool. Also s1 is always pointing to heap object.
Step 2:
By using heap object reference s1, we are trying to get corresponding string constant pool object referenc s2, using intern()
Step 3:
Intentionally creating a object with data 'Rakesh' in string constant pool, referenced by name s3
As "==" operator meant for reference comparison.
Getting false for s1==s2
Getting true for s2==s3
Hope this help!!