As the name suggests, String Pool in java is a pool of Strings stored in Java Heap Memory. We know that String is a special class in java and we can create String objects using a new operator as well as providing values in double-quotes.
Here is a diagram that clearly explains how String Pool is maintained in java heap space and what happens when we use different ways to create Strings. String Pool is possible only because String is immutable in Java and its implementation of String interning concept. String pool is also example of Flyweight design pattern. String pool helps in saving a lot of space for Java Runtime although it takes more time to create the String. When we use double quotes to create a String, it first looks for String with the same value in the String pool, if found it just returns the reference else it creates a new String in the pool and then returns the reference. However using new operator, we force String class to create a new String object in heap space. We can use intern()
method to put it into the pool or refer to another String object from the string pool having the same value. Here is the java program for the String Pool image:
package com.journaldev.util;
public class StringPool {
/**
* Java String Pool example
* @param args
*/
public static void main(String[] args) {
String s1 = "Cat";
String s2 = "Cat";
String s3 = new String("Cat");
System.out.println("s1 == s2 :"+(s1==s2));
System.out.println("s1 == s3 :"+(s1==s3));
}
}
Output of the above program is:
s1 == s2 :true
s1 == s3 :false
Recommended Read: Java String Class
Sometimes in java interview, you will be asked a question around String pool. For example, how many strings are getting created in the below statement;
String str = new String("Cat");
In the above statement, either 1 or 2 string will be created. If there is already a string literal “Cat” in the pool, then only one string “str” will be created in the pool. If there is no string literal “Cat” in the pool, then it will be first created in the pool and then in the heap space, so a total of 2 string objects will be created. Read: Java String Interview Questions
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
hi … excellent post … one correction - “we force String class to create a new String object and then place it in the pool.” here, new operator creates object in Java heap, not in pool.
- abhay agarwal
Where the string stored.Means location of string where it stored.
- Ridham Shah
Could you post some example on intern() method.
- siddu
https://stackoverflow.com/questions/17489250/how-can-a-string-be-initialized-using/17489410#17489410
- Vikash
I have one confusion, when we create a new String Object like String s = new String (“abc”); As far as I know, this will create a new String Object in Heap and also abc will go in String constant pool and s will point value exists in string constant pool… which means the value already exists in String Constant Pool, so what will intern do in this case? Also String Constant Pool is not a part of Java Heap Memory…?
- kunal
How come String Pool has two Cat literals and is Stringpool a part of Heap?
- Ganesh
String s = “ashish”; String b = new String(“ashish”); b.intern(); System.out.println(s==b); if string pool is manged then why not string ‘s’ reference is returned on b.intern() to b. It is printing false which means their reference are not same.
- Ashish
If for example: String str1 = “abc”; String str2 = new String(“def”); Then, Case 1: String str3 = str1.concat(str2) will go in the heap or pool? Case 2: String str4 = str2.concat(“HI”) will go in the heap or pool?
- krishna
public class MainDemo { public void comp() { String s1=“India”; String s2=“India”; String s3=new String(“India”); System.out.println(“== result:” + s1==s2); // false System.out.println(“equals result:” + s1.equals(s2)); // true System.out.println(“****************”); System.out.println(“== result:” + s1==s3); // false System.out.println(“equals result:” + s1.equals(s3)); // true } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub MainDemo obj=new MainDemo(); obj.comp(); } } Question - what is the difference between s1==s2 and s1==s3 , => both returns same result. but in your post s1==s2 is true that is not right result. I have checked this by writing same program in to this post. So plz clear my doubt ??
- sanjaya verma
Thnx for the post. helped to clarify a lot of things
- balinder