Computing Atlas

How Computing Was Built
Sign In
Text size
100%
Theme
Algorithm

String interning

Algorithm Classification

String interning is a method of storing only one copy of each distinct string value, which requires that strings be immutable. The distinct values are kept in a string intern pool, and the single stored copy of each string is called its intern. Interning trades extra time when a string is created for faster comparison and lower memory use later: once strings are interned, comparing two of them reduces to a single pointer equality test instead of examining every character. Languages such as Java, Python, PHP, Lua and the .NET languages support it, and Lisp, Scheme, Julia, Ruby and Smalltalk have symbol types that are essentially interned strings. Compilers, dynamic language runtimes and XML parsers rely on it for names and keys. The practical issues are that a global intern pool needs synchronization under multithreading, which can be reduced by partitioning the pool, and that unused interned strings may need a garbage collector with weak references to be reclaimed. Lisp introduced the idea for its symbols.

Connections

In Field

Source String interning (Wikipedia)
Sources
String interning (Wikipedia)
  • In Group: Programming Patterns and Approaches, Lead
    string interning is a method of storing only one copy of each distinct string value, which must be immutable
  • In Field: Programming Language Theory, Lead
    String interning is supported by some modern object-oriented programming languages, including Java, Python, PHP (since 5.4), Lua
View the Source
Comments (0)
No comments yet. Be the first to share a thought.
Reader Challenges (0)
No disputes yet. Spotted an error or a better source? Open the first one.