Back to Java Questions Index

Cosmic Q&A

What is a String pool in Java?

Answer

When we create a String in Java with double quotes, and that String is already present in JVM heap memory, JVM points the new String to already existing String in memory, since both the Strings refer to the same data. This becomes possible because String is immutable. If an operation is performed on any of the Strings, JVM will create a new String. Pooling of Strings like this in Heap leads to memory efficiency. We can force JVM to create a new String rather than use from pool using new keyword.


Share this page:

Report Error in Content