why string constant pool in java

Answers were Sorted based on User's Feedback



why string constant pool in java..

Answer / loknathreddy

String constant pool is a separate block of memory where
String object are held by JVM.
If a String object is created directly by using assignment
operator(=) as
String s1="Hello";
then it is stored in String Constant pool.
One of important characteristic of String constant pool is
that it doesn’t create same String object if there is
already in the String constant pool.

String s1 = “Hello”;
String s2 = “Hello”;

For above two String objects, JVM creates only one object
in the String constant pool and for the second string
reference variable (s2) will created, it points the string
object which is created for s1. In this case, (s1 == s2) is
true.
but when we use new operator to create object then this
object will store in heap memory.. be carefull on this.

Is This Answer Correct ?    26 Yes 2 No

why string constant pool in java..

Answer / shabina

To make Java more memory efficient, the JVM set aside a special area of memory called as "String Constant Pool" or "String Literal Pool".
When the compiler encounters a string literal, it checks the pool to see if an identical strong already exists or not, If not then it creates a new string literal object.
A String object is created out of the string constant pool, even if an equal already exists in the pool. Considering all that avoid new string unless you specially know that you need it.
JVM has a string pool where it keeps at most one object of any string. String literals always refers to an object in the string literal pool.
String objects created with the new string do not refer to objects in the string pool but can be made to using string's intern() method.
the java.lang.string.intern() returns an intended string, that is one that has an entry in the global string pool, then it will be added.
there is a table always maintaining a single reference to each unique object int the string literal pool ever created by an instance of the runtime in order to optimize space.
That means that they always have a reference to string objects in string literal pool. Therefore, the string objects in the string literal pool are not eligible for garbage collection.

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More Core Java Interview Questions

Differentiate between the constructors and methods in java?

0 Answers  


What is Runtime class and its purpose?

2 Answers  


How does Vector implement synchronization?

4 Answers   Ness Technologies,


What is size () in java?

0 Answers  


How to decrease number of hashings in has

1 Answers  






What is the use of singleton class?

0 Answers  


take any 4 input numbers. like 2345.. wanted out put is All 16 combinations of the number 2345.. for example- taking input as 4565 output- 5654 4556 4655..

2 Answers   Emphasis,


How can u increase the heap size in the memory?

0 Answers   Infosys,


What is object cloning in Java?

0 Answers   SwanSoft Technologies,


Explain what are final variable in java?

0 Answers  


Is vector ordered in java?

0 Answers  


what is for datainputstream?

1 Answers  


Categories