Recent Tutorials and Articles
    String pool and Intern String Objects
    Published on: 4th August 2013

    This tutorial details String pool and Intern String Objects along with how to intern the String objects i.e. push them into String pool.

    In Java, String object is immutable and hence can not be modified once created. A basic traditional problem with immutable objects is more memory consumption as for any change(small or significant), you need to create a different object resulting into saving the same data multiple times in memory.

    A sound and tested design pattern to resolve this problem is Object pooling i.e. creating a pool where the immutable objects can be saved and if anytime, an object with same data is required, it can be reused from pool. Java also does pooling for String Objects to optimize the memory consumption.

    How do we add/get our String objects to/from the pool?

    As you would be aware that there are following 2 ways to create String Objects -

    1. Short hand/Literal notation - e.g. String str = "Literal notation";
    2. new operator - e.g. String str = new String("String using new operator");

    String objects created using short hand/liternal notation are automatically managed in String pool while the others are not.

    StringPoolDemo.java
    package com.sts.allprogrammingtutorials.javabasics;
    
    /**
     * @author Sain Technology Solutions
     *
     */
    public class StringPoolDemo {
    	
    	public static void main(String[] args) {
    		//String Objects created using literal/short hand notation are reused from pool if exists, 
    		//otherwise a new String Object is created and pushed into the String pool 
    		String str = "String Object in pool!";
    
    		//Since str1 is created using literal notation and have same value as str, 
    		//it would be reused from the pool rather than creating a new object
    		String str1 = "String Object in pool!";
    
    		//String Objects created using new operator are always created so they don't interact with String pool
    		String str2 = new String("String Object in pool!");
    		
    		System.out.println("{str == str1}: " + (str == str1));
    		
    		System.out.println("{str == str2}: " + (str == str2));
    	}
    }
    
    

    Here is the output that you will see after running the program -

    Output:
    {str == str1}: true
    {str == str2}: false
    
    

    As you can see that str and str1 created using short hand notation are pointing to same String instance. However str3 created using new operator is pointing to a different instance.

    What are Intern Strings?

    String objects residing in the pool are called Intern String objects. Therefore String objects created using literal notation are Intern String objects.

    How to make String objects intern?

    Java has introduced in String class a method called intern() which pushes the String object in pool if doesn't exist already. Let's have a look on below code where we make str2 intern before comparison -

    StringPoolDemo.java
    package com.sts.allprogrammingtutorials.javabasics;
    
    /**
     * @author Sain Technology Solutions
     *
     */
    public class StringPoolDemo {
    	
    	public static void main(String[] args) {
    		//String Objects created using literal/short hand notation are reused from pool if exists, 
    		//otherwise a new String Object is created and pushed into the String pool 
    		String str = "String Object in pool!";
    
    		//Since str1 is created using literal notation and have same value as str, 
    		//it would be reused from the pool rather than creating a new object
    		String str1 = "String Object in pool!";
    
    		//String Objects created using new operator are always created so they don't interact with String pool
    		String str2 = new String("String Object in pool!");
    		
    		System.out.println("{str == str1}: " + (str == str1));
    		
    		System.out.println("{str == str2}: " + (str == str2));
    		
    		str2 = str2.intern();
    		System.out.println("\nAfter making str2 intern - ");
    		System.out.println("{str == str2}: " + (str == str2));
    	}
    }
    
    

    Here is the output that you will see after running the program -

    Output:
    {str == str1}: true
    {str == str2}: false
    
    After making str2 intern - 
    {str == str2}: true
    
    

    Thank you for reading through the tutorial. In case of any feedback/questions/concerns, you can communicate same to us through your comments and we shall get back to you as soon as possible.

    Published on: 4th August 2013

    Comment Form is loading comments...