String Pool and Interning

Overview

String pool is a storage space in Java heap memory where string literals are stored. This is maintained by the Java String class. The string pool is empty by default. String Pool improves performance and memory load by reducing the number of String objects created in JVM.

Aim

  • Help understand the concept and need of String Pool and String. intern().

  • Explain changes when strings are created using new keyword and string literal.

  • Explain intern works

Introduction

A String in Java is immutable. Immutable means that you cannot change or alter a string after creating it. If you try to alter a new object will be created in the heap with the altered changes.

A String is a set of Characters that are always enclosed in double quotes. In Java String a treated as an Object.

Whenever a string literal is created it is stored in a special place called String Pool in Heap memory. The JVM checks the String Pool before creating a new String Object if the literal is found JVM will return a reference to it else it will go on creating an object.

String str = "String pool";
///Creating s String Literal

String Pool Memory Allocation

  • By default, the String Pool is empty and maintained by String Class

  • When a string literal is created the JVM checks the String Pool and if the literal is found it will just return the reference to it and that reference will be stored in a variable

  • In case the literal is not found the JVM will create a new Object in the String Pool and returns its reference which will be stored in a variable

What's the need for String Pool

  • Suppose we are creating n number of strings with the same value then a distinct object will be created each time and distinct memory is allocated to each object.

  • Due to this, there will be inefficient usage of heap memory. To improve performance and reduce memory usage, JVM optimizes how strings are stored with help of the String Pool

Creating String with New keyword and intern

  • When creating the string using a new keyword even though the string value is present in the String Pool, it will create a separate object in the heap.

  • To avoid this we can invoke the intern method on the string. On invoking the intern method the JVM puts the string literal in the String Pool (if not present) and the reference is stored in a variable. If already present then it will just return the reference to it.

  • The above and the Below diagram will illustrate the difference.

String str1 = "Hello";
String str2 = "Hello";
String str3 = "Helllo";

String s4 = new String("Hello");
String s5 = new String("Hello");

s4.intern();
s5.intern();

Advantages and Disadvantages

  • String Pool reduces memory usage and improves performance by caching the string

  • The string can be reused instead of creating each time by just returning the reference

  • Since strings are immutable altering it will create a new object in the heap leading to memory waste.

Did you find this article valuable?

Support Mohammed Sharooque by becoming a sponsor. Any amount is appreciated!