Tuesday, July 16, 2019

String Pool in Java

String in Java is one of the most used class so there are certain optimizations done to make Strings more efficient, one of that optimization is String pool in Java which reduces the memory usage of the String objects. Use of the string pool is possible because of another design decision; making String immutable in Java.

Java String pool

As the name itself suggests String pool is a pooling of String objects. So, the question is on what basis Strings are pooled and how does it reduces memory usage?

In more technical term it can be explained as when JVM sees a String literal it interns the created String object i.e. stores it in a pool that has only one such copy of that String.

To explain it in detail we’ll have to start from the beginning!

As you must be knowing there are two ways to create a String in Java–

  • Using String literal
  • Using new keyword
When you create a String using String literal, for example-
String str = “abc”

Memory allocation for this string object happens in an area knows as string pool in Java which is part of the heap memory.

Java String pool

When you create any String literal, JVM searches the String pool for any String having the same value, if such a value is found reference to the same memory is returned. So, new memory is not allocated for the same string rather the reference is shared among the String literals having the same value.

If String with same value is not found in the pool the new value is added to the pool and its reference is returned.

For example, if two Strings are created as follows-

String str1 = “abc”;
String str2 = “abc”;

Then the reference is shared by both the objects.

You can also check it using a Java program by creating two String literal having same value and then compare their references using equality ‘==’ operator, if true is returned that means both have the same reference.

public class StringDemo {

 public static void main(String[] args) {
  String str1 = "abc";
  String str2 = "abc";
  if(str1 == str2){
   System.out.println("str1 and str2 are same");
  }else{
   System.out.println("str1 and str2 are not same");
  }
 }
}

Output

str1 and str2 are same

String created using new operator

Where as when you create a String object using new operator, it always creates a new object in heap memory.

For example, if three Strings are created as follows-

String str1 = “abc”;
String str2 = new String(“abc”);
String str3 = new String(“abc”);

Then str1 is stored in a pool where as new objects are created for str2 and str3.

string interning

Here is a Java example to check the same. If we create two more strings using new operator and then compare reference those references should be different.

public class StringDemo {
 public static void main(String[] args) {
  // Literals
  String str1 = "abc";
  String str2 = "abc";
  if(str1 == str2){
   System.out.println("str1 and str2 are same");
  }else{
   System.out.println("str1 and str2 are not same");
  }
  // Using new operator
  String str3 = new String("abc");
  String str4 = new String("abc");
  if(str3 == str4){
   System.out.println("str3 and str4 are same");
  }else{
   System.out.println("str3 and str4 are not same");
  }
  
  if(str1 == str4){
   System.out.println("str1 and str4 are same");
  }else{
   System.out.println("str1 and str4 are not same");
  }
 }
}

Output

str1 and str2 are same
str3 and str4 are not same
str1 and str4 are not same

Here it can be seen that str3 and str4 are having separate reference as those strings are created using new operator.

Explicitly interning a String

You can explicitly intern a string using intern() method in Java.

As per Java docs- “When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.”

In the previous example if str4 is changed and uses intern() method then str4 should also return a reference a similar to str1 and str2.

public class StringDemo {

 public static void main(String[] args) {
  String str1 = "abc";
  String str2 = "abc";
  if(str1 == str2){
   System.out.println("str1 and str2 are same");
  }else{
   System.out.println("str1 and str2 are not same");
  }
  String str3 = new String("abc");
  String str4 = new String("abc").intern();
  if(str3 == str4){
   System.out.println("str3 and str4 are same");
  }else{
   System.out.println("str3 and str4 are not same");
  }
  
  if(str1 == str4){
   System.out.println("str1 and str4 are same");
  }else{
   System.out.println("str1 and str4 are not same");
  }
 }
}

Output

str1 and str2 are same
str3 and str4 are not same
str1 and str4 are same

It can be seen that str1 and str4 are having the same reference now.

String pool values are garbage collected

Strings in the Java string pool are eligible for garbage collection if there are no references to them.

That's all for this topic String Pool in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related topics

  1. How to Create Immutable Class in Java
  2. Java String Interview Questions And Answers
  3. Java String Search Using indexOf(), lastIndexOf() And contains() Methods
  4. String Vs StringBuffer Vs StringBuilder in Java
  5. How to Find The Longest Palindrome in a Given String - Java Program

You may also like-

  1. Java for Loop With Examples
  2. Abstract Class in Java
  3. TreeSet in Java
  4. Thread States (Thread Life Cycle) in Java Multi-Threading
  5. Java Stream API Tutorial
  6. DataSource in Java-JDBC
  7. Spring Web MVC Tutorial
  8. Class And Object in Python