Questions tagged [string-interning]

A string pool allows a runtime to save memory by preserving immutable strings in a pool, so that instances of common strings can be reused throughout the application instead of creating multiple instances of them.

In computer science, 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.

163 questions
287
votes
8 answers

What is Java String interning?

What is String Interning in Java, when I should use it, and why?
saplingPro
  • 20,769
  • 53
  • 137
  • 195
198
votes
14 answers

When should we use intern method of String on String literals

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…
Rakesh Juyal
  • 35,919
  • 68
  • 173
  • 214
118
votes
2 answers

Python string interning

While this question doesn't have any real use in practice, I am curious as to how Python does string interning. I have noticed the following. >>> "string" is "string" True This is as I expected. You can also do this. >>> "strin"+"g" is…
Ze'ev G
  • 1,646
  • 2
  • 12
  • 15
94
votes
4 answers

Why do (only) some compilers use the same address for identical string literals?

https://godbolt.org/z/cyBiWY I can see two 'some' literals in assembler code generated by MSVC, but only one with clang and gcc. This leads to totally different results of code execution. static const char *A = "some"; static const char *B =…
57
votes
5 answers

String interning in .NET Framework - What are the benefits and when to use interning

I want to know the process and internals of string interning specific to .NET Framework. Would also like to know the benefits of using interning and the scenarios/situations where we should use string interning to improve the performance. Though I…
S2S2
  • 8,322
  • 5
  • 37
  • 65
55
votes
9 answers

intern() behaving differently in Java 6 and Java 7

class Test { public static void main(String...args) { String s1 = "Good"; s1 = s1 + "morning"; System.out.println(s1.intern()); String s2 = "Goodmorning"; if (s1 == s2) { …
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
52
votes
3 answers

Do common JavaScript implementations use string interning?

Do common JavaScript engines, such as V8 and WebKit's JavaScriptCore, use string interning for JavaScript strings? Or do they actually keep multiple instances of identical strings in memory?
kpozin
  • 25,691
  • 19
  • 57
  • 76
40
votes
4 answers

When to use intern() on String literals

I see a lot of legacy code like this: class A { public static final String CONSTANT = "value".intern(); ... } I don't see any reason for the intern(), as in the Javadoc one can read: "All literal strings and string-valued constant…
Abdul
  • 611
  • 7
  • 11
31
votes
11 answers

Behavior of String literals is confusing

The behavior of String literals is very confusing in the code below. I can understand line 1, line 2, and line 3 are true, but why is line 4 false? When I print the hashcode of both they are the same. class Hello { public static void…
user2416387
24
votes
5 answers

Real Life, Practical Example of Using String.intern() in Java?

I've seen many primitive examples describing how String intern()'ing works, but I have yet to see a real-life use-case that would benefit from it. The only situation that I can dream up is having a web service that receives a considerable amount of…
Tom N
  • 241
  • 2
  • 3
21
votes
7 answers

Is string interning really useful?

I was having a conversation about strings and various languages a while back, and the topic of string interning came up. Apparently Java and the .NET framework do this automatically with all strings, as well as several scripting languages. …
Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
20
votes
2 answers

Which debugging tool can list strings internalized?

I am looking to a debugging tool that can list the strings that have been internalized? Ideally, I would like to put a mark and have a list of the strings that been added after that mark. Thanks in advance.
Guillaume Coté
  • 2,761
  • 2
  • 21
  • 28
20
votes
3 answers

The return of String.intern() explained

Consider: String s1 = new StringBuilder("Cattie").append(" & Doggie").toString(); System.out.println(s1.intern() == s1); // true why? System.out.println(s1 == "Cattie & Doggie"); // true another why? String s2 = new…
Hearen
  • 7,420
  • 4
  • 53
  • 63
19
votes
4 answers

What is the purpose of casting into "object" type?

I have found code on a website which is as follows. string a = "xx"; string b = "xx"; string c = "x"; string d = String.Intern(c + c); Console.WriteLine((object)a == (object)b); // True Console.WriteLine((object)a == (object)d); //…
babybob
  • 444
  • 6
  • 22
18
votes
2 answers

Why are annotation string values not interned?

The following snippet prints 4 distinct hash codes, despite reusing a string constant and literal. Why are string values not interned on annotation elements? public class Foo { @Retention(RetentionPolicy.RUNTIME) @interface Bar { …
shmosel
  • 49,289
  • 6
  • 73
  • 138
1
2 3
10 11