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:
- How Serial Garbage Collector works in the HotSpot virtual machine
- What flavors of garbage collectors are implemented in the HotSpot virtual machine
- How does the garbage collector work in Java
Comments
Post a Comment