String pool in Java

String pool in Java

String pool in Java is a feature of JVM that is the implementation of the concept String Interning. It also knows as string constant pool. It’s a place in HEAP memory to store string literal.

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.

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() in Java. All compile-time constant strings in Java are automatically interned using this method.

Wikipedia.com

The benefits of String Pool in java

Before jumping into how String Pool works we should have a look at the benefit of Spring Pool.

String interning speeds up string comparisons, which are sometimes a performance bottleneck in applications (such as compilers and dynamic programming language runtimes) that rely heavily on associative arrays with string keys to look up the attributes and methods of an object. Without interning, comparing two distinct strings may involve examining every character of both. This is slow for several reasons: it is inherently O(n) in the length of the strings; it typically requires reads from several regions of memory, which take time; and the reads fill-up the processor cache, meaning there is less cache available for other needs. With interned strings, a simple object identity test suffices after the original intern operation; this is typically implemented as a pointer equality test, normally just a single machine instruction with no memory reference at all.

String interning also reduces memory usage if there are many instances of the same string value; for instance, it is read from a network or from storage. Such strings may include magic numbers or network protocol information. For example, XML parsers may intern names of tags and attributes to save memory. Network transfer of objects over Java RMI serialization object streams can transfer strings that are interned more efficiently, as the String object’s handle is used in place of duplicate objects upon serialization.

The motivation for applying string interning: wikipedia.com

How does String Pool work in Java?

In java, when we initialize a String variable by literal. JVM will store that string in the String Pool. Afterward, if when we initialize other String variables with the same value. JVM is not allocating a new memory block to store. Those variables will be assigned the same reference with the first. By doing this way, we can use == to equal two String variables.

Also, we can intern String object from outside to String Pool by calling intern(). Let take an example:

String outsidePool = new String("New String object using new keyword");
outsidePool.intern();
String anotherString = "New String object using new keyword"; System.out.print(outsidePool == anotherString ); // true

Take a look at java docs to have an explanation on the usage of: the method returns a canonical representation for the string object. When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.