String pool in Java

A String pool is a collection of strings stored in Heap.

  • String pooling is possible thanks to Java's immutability of strings and the implementation of the idea of string interning;
  • Pooling of strings helps save memory, but for the same reason, string creation takes longer;
  • When "is used to create a string," is first searched for a string in the pool with the same value, if found, then a link is simply returned, otherwise a new string is created in the pool, and then a link to it is returned;
  • Using the new operator creates a new String object. Then, using the intern() method, this string can be put into the pool, or you can get from the pool a reference to another String object with the same value;
  • The string pool is an example of the Flyweight pattern.

Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed