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
11
votes
3 answers

Java - strings equals when decompiled

I decompiled some Java code the other day and found this: String s1 = "something"; String s2 = "something_else"; if (s1 == s2) { // Path 1 } else { // Path 2 } Obviously using '==' to test for string equality is bad But I wondered - This code has…
andy boot
  • 11,355
  • 3
  • 53
  • 66
10
votes
3 answers

Why Javascript ===/== string equality sometimes has constant time complexity and sometimes has linear time complexity?

After I found that the common/latest Javascript implementations are using String Interning for perfomance boost (Do common JavaScript implementations use string interning?), I thought === for strings would get the constant O(1) time. So I gave a…
10
votes
2 answers

Java string interning, what is guaranteed?

The question boils down to this code: // setup String str1 = "some string"; String str2 = new String(str1); assert str1.equals(str2); assert str1 != str2; String str3 = str2.intern(); // question cases boolean case1 = str1 == "some string"; boolean…
hyde
  • 60,639
  • 21
  • 115
  • 176
9
votes
2 answers

Why does the String.intern() method return two different results?

I have the code like this: String str1 = new StringBuilder("计算机").append("软件").toString(); System.out.println(str1.intern() == str1); //true String str2 = new StringBuilder("ja").append("va").toString(); System.out.println(str2.intern() == str2);…
Timi
  • 892
  • 1
  • 8
  • 17
9
votes
6 answers

String interning?

The second ReferenceEquals call returns false. Why isn't the string in s4 interned? (I don't care about the advantages of StringBuilder over string concatenation.) string s1 = "tom"; string s2 = "tom"; Console.Write(object.ReferenceEquals(s2,…
rkrauter
  • 1,159
  • 1
  • 11
  • 23
9
votes
1 answer

Any weak interning collections (for immutable objects)

In some situations involving immutable objects, it will be possible for many distinct objects to come into existence which are semantically identical. A simple example would be reading many lines of text from a file into strings. From the…
supercat
  • 77,689
  • 9
  • 166
  • 211
8
votes
2 answers

Use PermGen space or roll-my-own intern method?

I am writing a Codec to process messages sent over TCP using a bespoke wire protocol. During the decode process I create a number of Strings, BigDecimals and dates. The client-server access patterns mean that it is common for the client to issue a…
Adamski
  • 54,009
  • 15
  • 113
  • 152
8
votes
1 answer

Garbage collection on intern'd strings, String Pool, and perm-space

After exploring java's string internals I've grown confused on what is referred to as the "perm space." My understanding initially of it was that it held String literals as well as class meta data as explained in this question. I've also read about…
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
8
votes
1 answer

Alternatives to Java string interning

Since Java's default string interning has got a lot of bad press, I am looking for an alternative. Can you suggest an API which is a good alternative to Java string interning? My application uses Java 6. My requirement is mainly to avoid duplicate…
MoveFast
  • 3,011
  • 2
  • 27
  • 53
8
votes
5 answers

Why does .NET create new substrings instead of pointing into existing strings?

From a brief look using Reflector, it looks like String.Substring() allocates memory for each substring. Am I correct that this is the case? I thought that wouldn't be necessary since strings are immutable. My underlying goal was to create a…
foson
  • 10,037
  • 2
  • 35
  • 53
7
votes
2 answers

java string concatenation and interning

Question 1 String a1 = "I Love" + " Java"; String a2 = "I Love " + "Java"; System.out.println( a1 == a2 ); // true String b1 = "I Love"; b1 += " Java"; String b2 = "I Love "; b2 += "Java"; System.out.println( b1 == b2 ); // false In the first…
wli75
  • 83
  • 4
7
votes
3 answers

String comparison and String interning in Java

When should one compare Strings as objects and when should one use their equals method? To make sure, I always use equals, but that doesn't seem very efficient. In what situations can I be certain that string1 == string2 is a safe to use?
Albus Dumbledore
  • 12,368
  • 23
  • 64
  • 105
7
votes
11 answers

deadlock on synchronized ( String intern())

I user sun jdk 1.5 ThreadPoolExecutor( 24, 24,60,TimeUnit.SECONDS, new LinkedBlockingQueue()). soemtime I use jdb tool to find the status of all threads in thread pool are " waiting in a monitor", the code is : String key =…
user44230
  • 87
  • 1
  • 1
  • 4
7
votes
1 answer

How is Java's String#intern() method implemented?

I tried to look at Java's String#intern() method, but it's public native String intern(); In general, how is interning implemented? In String's case?
6
votes
2 answers

Intern string literals misunderstanding?

I dont understand : MSDN says http://msdn.microsoft.com/en-us/library/system.string.intern.aspx Consequently, an instance of a literal string with a particular value only exists once in the system. For example, if you assign the same literal…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
1 2
3
10 11