String interning

In computer science, string interning is a method of storing only one copy of each distinct string value, which must be immutable.[1] Interning strings makes some string processing tasks more time-efficient 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.

The single copy of each string is called its intern and is typically looked up by a method of the string class, for example String.intern()[2] in Java. All compile-time constant strings in Java are automatically interned using this method.[3]

String interning is supported by some modern object-oriented programming languages, including Java, Python, PHP (since 5.4), Lua[4] and .NET languages.[5] Lisp, Scheme, Julia, Ruby and Smalltalk are among the languages with a symbol type that are basically interned strings. The library of the Standard ML of New Jersey contains an atom type that does the same thing. Objective-C's selectors, which are mainly used as method names, are interned strings.

Objects other than strings can be interned. For example, in Java, when primitive values are boxed into a wrapper object, certain values (any boolean, any byte, any char from 0 to 127, and any short or int between −128 and 127) are interned, and any two boxing conversions of one of these values are guaranteed to result in the same object.[6]

  1. ^ "String.Intern Method (String)". Microsoft Developer Network. Retrieved 25 March 2017.
  2. ^ String.intern()
  3. ^ "Chapter 15. Expressions". docs.oracle.com. Retrieved 30 January 2019.
  4. ^ "lua-users wiki: Immutable Objects". lua-users.org. Retrieved 30 January 2019.
  5. ^ rpetrusha. "String Class (System)". docs.microsoft.com. Retrieved 30 January 2019.
  6. ^ "Chapter 5. Conversions and Promotions". docs.oracle.com. Retrieved 30 January 2019.

© MMXXIII Rich X Search. We shall prevail. All rights reserved. Rich X Search