article

In some modern programming languages, including Java and C#, the string intern pool is a data structure managed internally by the platform or virtual machine to facilitate efficient implementation of certain string processing tasks. The pool contains a single copy (called the intern) of each distinct string that is currently represented by a string object in the system. By invoking a method of the string class (for example in Java), the programmer has access to this unique string object.

The most common use for the string intern pool is for doing large numbers of string comparisons. For example, if we have n strings and we wish to find any duplicates, O(n^{2}) string comparisons are needed. Unfortunately, string comparison can be slow (especially if the strings are long and don't vary much), and this many string comparisons may be unacceptable. The solution is to use the string intern pool to get the intern of each of these strings, and then we only need to do O(n^{2}) reference comparisons, which are much faster. Even if retrieving the interns is fairly slow (and with proper implementation, e.g. using a hash table, it need not be), only O(n) of these operations must be performed, meaning that for large enough n, using the intern pool will be faster.

One disadvantage of the string intern pool is that since every string object must be checked against the pool at creation, there is some performance penalty even if the intern pool is never used by the application. However, due to the possible benefits, this is generally considered acceptable in the languages in which it is implemented.

Optimizations

 

This article is licensed under the GNU Free Documentation License. It uses material from the "String intern pool".

Home Pageartsbusinesscomputersgameshealthhospitalshomekids & teensnewsphysiciansrecreationreferenceregionalscienceshoppingsocietysportsworld