HakiDocs
Java

String Pool

Space in the Heap for Strings

One of the most
used features in Java
is the Java String.

That's why the JVM
has created a special
galaxy in its universe
called String Pool for
storing Strings.

The String Pool lives
also inside the Heap Space.

The reason for this
is basically for optimising
as much as possible the
creation of Java String
since every string object
is immutable.

For searching a match,
the java compiler is the
one checking the literals
and Compiler Time Constants .

The new keyword

Using the new() operator for the String, creates always a unique and different object in heap memory. Even if it is exactly the same as other literal or object.

String first = "Baeldung"; 
String second = "Baeldung"; 
System.out.println(first == second); // True :: Compiler has found a match on String pool

String third = new String("Baeldung");
String fourth = new String("Baeldung"); 
System.out.println(third == fourth); // False :: Each object is unique. 

Since one of the
benefits of Java is
causing less memory leaks,
the String Pool is stored in
the Heap Space, where the
Garbage Collector may
remove unreferenced Strings.

Resources


On this page