Javatpoint Logo

what is string pool and string constant pool in java?

By: rajmsc100@gmail.com On: Mon Mar 17 11:59:28 IST 2014     Question Reputation5 Answer Reputation0 Quiz Belt Series Points2  7Blank User
Hi

Can any one explain to me what is string pool and string constant pool in java?


Thanks in a Advance

Regards
Raj kumar R
Up2Down

 
String pool is there in class context area...
whenever we are going to create String object, first JVM fetches that string object in in this pool,if that object exists then the reference of that object is send to the new object.if not exist it will create new object.
if we want to modify string also,it will not modify instead it creates new string object...
Image Created5Down

By: venkateswarlu1256@gmail.com On: Mon Mar 17 16:45:53 IST 2014 Question Reputation5 Answer Reputation19 Belt Series Points0 24User Image
Are You Satisfied :0Yes0No
 
When the compiler encounters a String literal, it checks the pool to see if an identical String already exists. If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created.

In this case, we actually end up with a slightly different behavior because of the keyword "new." In such a case, references to String literals are still put into the constant table (the String Literal Pool), but, when you come to the keyword "new," the JVM is obliged to create a new String object at run-time, rather than using the one from the constant table.
Image Created0Down

By: kumarrajat557@gmail.com On: Wed Mar 19 12:07:35 IST 2014 Question Reputation0 Answer Reputation359 Belt Series Points0 359User Image
Are You Satisfied :1Yes0No
 
String pool is a memory which resides in the heap area of JVM which is specifically used to store objects in Java, (heap is a kind of tree data structure). When a string literal is assigned to a variable (e.g. String str = "George") a new object is created for the literal in the string pool and a reference to this object is stored in the string constant pool (table). The JVM initially checks the string pool for the string literal before creating the above-said object and if it finds an object with the same value, a new object is not created instead only a reference to the existing object is returned to the string variable str. But in case we used the new operator and constructor to create a string (object), then along with performing the above procedures once again, JVM will be obliged to create a new string object in the string pool and return its reference to the string variable and also store a copy of the reference to the object in the string constant pool (table) once again.

String str1 = new String("Hello");
String str2 = new String("Hello");
String str3 = "Hello";
String str4 = "Hello";

str1 == str2 // false
str2 == str3 // false this point is very important the string literal object and the new object created with new are different objects
str3 == str4 // true
Image Created0Down

By: vishnuvijai@gmail.com On: Fri Sep 22 09:32:39 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No