Understanding String Objects, Heap, and String Constant Pool

Abhijit
3 min readMay 1, 2024

--

String Representation in Java In Java, strings are represented as objects of the String class. When you create a new string using the new operator, a new string object is allocated in the heap memory. This object holds the character array representing the string value, along with other metadata.

Example: Creating String Objects

String str1 = new String("Hello");
String str2 = new String("Hello");

In the above example, two separate string objects are created in the heap, each with its own memory allocation for the character array and metadata, even though they have the same string value (“Hello”).

String Literal and String Constant Pool Java uses a special area of the heap memory called the string constant pool to store string literals. When you create a string using a literal, like String str3 = "World";, the JVM checks if an identical string already exists in the string constant pool. If so, it reuses the existing string object; otherwise, it creates a new string object in the pool.

String Constant Pool in the Heap The string constant pool is not separate from the heap; rather, it is a specific region within the heap memory that is dedicated to storing string literals. This design helps improve memory efficiency by allowing string literals to be shared across the application.

The intern() Method The String class provides a method called intern() that can be used to get a reference to a string from the string constant pool. If the string already exists in the pool, intern() returns a reference to that existing string object. Otherwise, it adds the new string to the pool and returns a reference to it.

Example: Using intern()

String str4 = new String("Hello").intern();
String str5 = "Hello";
System.out.println(str4 == str5); // Output: true

In the above example, str4 is initially created as a new string object in the heap using new String("Hello"). However, when intern() is called, it checks the string constant pool (within the heap) for an identical string. Since "Hello" already exists in the pool (due to the string literal str5), intern() returns a reference to that existing string object from the pool. As a result, str4 and str5 now reference the same string object, and str4 == str5 evaluates to true.

Representation of String Constant Pool concept in java.

Memory Efficiency with intern() Using intern() can help optimize memory usage by reducing the number of duplicate string objects in the heap. However, it's important to use it judiciously, as excessive or inappropriate use of intern() can introduce performance overhead and potentially negate the memory savings.

Heap and String Constant Pool Interaction The string constant pool is a specific region within the heap memory, dedicated to storing string literals. When a string object is created in the heap (e.g., using new String("Hello")), the JVM checks if an identical string already exists in the constant pool. If so, the new string object's character array is shared with the existing string object in the pool, reducing memory usage.

Conclusion Understanding the differences between string object creation and the use of intern(), as well as the interaction between the heap and the string constant pool (which is a part of the heap), is crucial for optimizing memory usage and writing efficient Java code involving strings. By leveraging the string constant pool and judiciously using intern(), you can minimize duplicate string objects and improve overall memory efficiency.

--

--

No responses yet