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

what is difference between interface and abstract class..?

4 Answers  


How do you sort data in java?

0 Answers  


What is the difference between the reader/writer class hierarchy and the inputstream/outputstream class hierarchy in java programming?

0 Answers  


Explain the key functions of data binding?

0 Answers   Akamai Technologies, Aspire, Impetus, Infogain, Tavant Technologies, Virtusa,


What are the types of methodology?

0 Answers  






Can you pass functions in java?

0 Answers  


Why strings in java are called as immutable?

0 Answers  


What is a generic code?

0 Answers  


When a thread is executing synchronized methods , then is it possible to execute other synchronized methods simultaneously by other threads?

0 Answers  


when System.out.println("") is executed what happens in the back ground?

2 Answers  


How do you write methodology?

0 Answers  


What is file in java?

0 Answers  


Categories