What is String Pool?

Questions by sha_kr2001   answers by sha_kr2001

Showing Answers 1 - 30 of 30 Answers

prashantB

  • Dec 27th, 2007
 

A JVM has a string pool where it keeps at most one object of any String. String literals always refer to an object in the string pool. String objects created with the new operator do not refer to objects in the string pool.

prashantB

  • Dec 27th, 2007
 

Ex1:

public class Program
{
public static void main(String[] args)
{
String str1 = "Hello";
String str2 = "Hello";
System.out.print(str1 == str2);
}
}

The result is

true


Ex2: public class Program
{
public static void main(String[] args)
{
String str1 = "Hello";
String str2 = new String("Hello");
System.out.print(str1 == str2 + " ");
System.out.print(str1.equals(str2));
}

....... hope this will help
}

The result is

false true

in java for String there is a special consideration, if you are creating a String class object like
String s="jai"
than this will go to string pool and if are again want the same string like
String s2="jai"
than no other object of String class is created now refrence varible s2 will refere to "jai"
this is done because String objects are immutable.
and you can check it by == test like
if(s==s2)
this will give the true value and exectue if condition.


means when you do any modificattion on them than another object of String class is created and the reference of that class is gone.like
String s="jai"
s=s.concat(" hai");
now is refering to jai hai and refrence to jai string is gone and this object is stored is sting pool. for further use.
like in future we need that object than the new object is not created and by jvm the refrence of that object is given to that refrence varible.

and if you are creating a object by new operator than always new object is created .
e.g.
String s2="jai".
String s=new String ("jai");
 and now you are doing the == test than this will give false and if condition will not execute.
if(s2==s)
{
//code
}
this will not execute.

sampra

  • Feb 11th, 2008
 

A JVM has a string pool where it keeps at most one object of any String. String literals always refer to an object in the string pool. String objects created with the new operator do not refer to objects in the string pool.Afetr using the string object returns to the pool in the pool there is no rool of gc.and object in the string pool is immutable

pankaj4mhp

  • Jan 21st, 2010
 

A JVM has the Pool. All the String Object which are created by assignment stored in the pool. This pool is present in the heap. So whenever any assignment is done for String first it check in the String Pool whether that String is already exist or not... This is done by calling intern() method present in the String class. If it find the same String then it return the same reference else it create new 1. But with new Operator everytime it creates the new Object.


eg.

String str = "S1" // first check wheather its present in the pool
String str1 ="S1"

So now str and str1 have the same reference so .equals and == method will return true.

String str2 = new String("S1");// this will create the new object in heap.

So above only 2 objects are created1 by str and other by str2

  Was this answer useful?  Yes

prashant patil

  • Sep 2nd, 2011
 

Simple String variable is stored in String Pool area, and string object is stored in heap area,
But bort can give you same result in hash code concept.

Ex:
String s="RAM";
String s2="RAM"

In to this case s2 is pointing to previous RAM it will not create new RAM in pool.

  Was this answer useful?  Yes

sampra

  • Mar 13th, 2012
 

A JVM has a string pool where it keeps at most one object of any String. String literals always refer to an object in the string pool. String objects created with the new operator do not refer to objects in the string pool.

  Was this answer useful?  Yes

Sameera

  • Sep 21st, 2017
 

String Pool in Java corresponds to an allocation of memory in Java heap memory. It consists of a collection of String objects, which are shared and reused among several String object references for same String content.

Note : This capability is gained through the immutability nature of Java String.

I have written an article with some more insights here on the topic
devdummy . com/2017/09/what-is-java-string-pool . html

Hope this helps.

  Was this answer useful?  Yes

Gurumurthy Ramamurthy

  • Mar 12th, 2018
 

String Pool or String Constant Pool or String Literal Pool refers to an area of HEAP where the String objects are stored. Once the String object is created in this area, cant be removed by garbage collection, the objects will be there till the end of the application. Since Strings are most widely used classes / data types in any application, by having less number of String objects, JVM will respond multiple clients requests. This is just to avoid/decrease object creation. Object creation is an expensive task, since Strings are most widely used, JVM has optimized the usage of String classes. Once you assign a value to a String, it cant be changed.

  Was this answer useful?  Yes

Damanpreet Singh

  • Sep 4th, 2018
 

String Pool is a separate memory location in Class Information Area which is differ from Heap. Anything which we write in double quotes always stored in String Pool

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions