what is meant by string pooling?

Answers were Sorted based on User's Feedback



what is meant by string pooling?..

Answer / ranganathkini

A string pool is a collection of references to String
objects. Strings, even though they are immutable, are still
objects like any other in Java. Objects are created on the
heap and Strings are no exception. So, Strings that are part
of the "String Literal Pool" still live on the heap, but
they have references to them from the String Literal Pool.

When a .java file is compiled into a .class file, any String
literals are noted in a special way, just as all constants
are. When a class is loaded (note that loading happens prior
to initialization), the JVM goes through the code for the
class and looks for String literals. When it finds one, it
checks to see if an equivalent String is already referenced
from the heap. If not, it creates a String instance on the
heap and stores a reference to that object in the constant
table. Once a reference is made to that String object, any
references to that String literal throughout your program
are simply replaced with the reference to the object
referenced from the String Literal Pool.

Is This Answer Correct ?    37 Yes 4 No

what is meant by string pooling?..

Answer / gaurav agrawal

dont misguide please, all the string objects created
without new operator are stored in pool which is a space in
memory, if any other object has the same content then a new
object will not be created.

Is This Answer Correct ?    35 Yes 2 No

what is meant by string pooling?..

Answer / indrajit yadav

A string pool is a collection of references to String
objects."String Literal Pool" still live on the heap memory,
but they have references to them from the String Literal Pool.

See the below example:
public class Program {
public static void main(String args[]){
//String literal will store in String pool
String s1="Indrajit";//case 1
String s2="Indrajit";//case 2
//In case 1, iteral s1 is created newly and kept in the
pool. But in case 2, literal s2 refer the s1, it will not
create new one instead
//String object
String s = new String("hi");
String ss = new String ("hi");
if (s1==s2){
System.out.print("equal");//print equal
}
/*
if(s==ss){
System.out.print("Reference are not equal");//no ouptput
}
*/
}
}

Is This Answer Correct ?    24 Yes 1 No

what is meant by string pooling?..

Answer / birendra kumar

String constant pool is part of the heap memory. which is
manage the memory efficient by the JVM.

if you create String literal like..
//suppose pool is empty..

// str(is reference variable) and abc(is String object)

String str="abc";

/*Creating on String object and one reference variable.
so abc is stored in pood and str is refered it. */

//if you created another String literal with same content like..
String str1="abc";

/* it is only creating reference variable which is point
same String object "abc". because of JVM is checking first
if any content with same string object in the pool just it
is make the reference not created new String object*/

if you creating String Object using new keyword like..

String str2=new String("abc");

/* creating two object and one reference variable*/
because here we used new keyword and it is creating normal
object in the heap(non pool)memory.where 1st object is
stored and str is refered to it. and 2nd object is stored as
it is in string pool.

that is reasion in java if you match with str with str2
using (==) operator it will give you false result because of
(==) operator is match basis of reference variable which is
pointing same object if you use equals method then you get
true.basis of content of the reference variable in .

Is This Answer Correct ?    15 Yes 0 No

what is meant by string pooling?..

Answer / devarathnam

Hi... Basically string pooling is a part of the heap memory
all string literals will be storing in the string pool
memory.
for example: String s1="DEVARATHNAM";// This is a string
literal ,will store in the string pool .

Is This Answer Correct ?    17 Yes 3 No

what is meant by string pooling?..

Answer / sivadasan

See,

String s = new String("Shiva");

Its creating two instances and one reference.

In the Two instance one is stored in Constant pool and
another one is stored in temp' pool.

What about String s = "Shiva";

Anybody can, pls clarify...

Is This Answer Correct ?    4 Yes 3 No

what is meant by string pooling?..

Answer / kumar

in string constant pool one object will be created and share that object form all required references.
hence the main advantage of scp is memory is utilized and performancce will be improved

Is This Answer Correct ?    0 Yes 0 No

what is meant by string pooling?..

Answer / vijay srivastava

Most of the Answers are correct but they are not much clear so I am trying write clear Answer. I hope, you guys will like it.

String pooling is the mechanism that was introduced by Java.
String pooling is possible because String is immutable and final class.

1- String firstString = "Hello";
2- String secondString = "Hello";
3- String thirdString = new String("Hello");
There is no guarantee to create new object for string Step 1 and 2 but in step 3, Jave(JVM) gives guarantee to create object.
In step 1st and 2nd, JVM search the same literal in String pool if it's available then return the reference else JVM will create the object for same and added in to String pool.
In step 3, JVM definitely create the new object, It will not part of String pool. If we want this newly created object should be part of String pool then we need to call intern() method of String class. If we called intern() method then JVM will search the same literal in String pool if JVM found same literal then return reference else JVM will add the same object in String pool.

Is This Answer Correct ?    0 Yes 0 No

what is meant by string pooling?..

Answer / ravikiran(aptech r&d)

so when we use a new operator an object will be created
right and every object will be save on garbage collectible heap.

Is This Answer Correct ?    3 Yes 4 No

what is meant by string pooling?..

Answer / dfd

dfdf

Is This Answer Correct ?    0 Yes 3 No

Post New Answer

More Core Java Interview Questions

What are different types of references?

0 Answers  


When we give defination of interface method in the class why method must be public???

2 Answers  


what is private constructor?what are the uses of writing private constructor in our program?

10 Answers  


What is the platform?

0 Answers  


What is difference between identifier and variable?

0 Answers  






Nullpointer exception is a very common exception. Why is it not made as a checked exception?

2 Answers   Ness Technologies,


Which collections are thread safe in java?

0 Answers  


What methodology can be employed to locate substrings inside a string?

0 Answers  


What is the use of parseint in java?

0 Answers  


In how many ways we can do synchronization in java?

0 Answers  


Is it necessary for the port addresses to be unique? Explain with reason.

0 Answers   Aricent,


What is bool mean?

0 Answers  


Categories