String interning is a technique used to store only one copy of each distinct string value in memory, regardless of how many times that string appears in your code or data structures.
A typical interning algorithm use an interning pool, such as an HashMap. When the string is interned:
- if the string already exists, a reference is returned
- if it doesn’t exist, it is added to the pool and the reference is returned
Applications will now internally only store the string reference, which will reduce in less memory usage, faster comparison, and immutability.