why string constant pool in java

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain about strings in java?

820


What is identifier with example?

797


What are different types of arrays?

740


What classes of exceptions may be caught by a catch clause in java programming?

930


Why Java doesn’t support multiple inheritance?

815


What is difference between ++ I and I ++ in java?

718


why java does not support unsigned keyword?

4704


What is map java?

733


Difference between a class and an object?

764


Which software is used for java programming?

773


List some features of the abstract class.

786


How to reverse string in java?

855


What's the base class in java from which all classes are derived?

1010


What do you understand by soft reference?

808


What are assembly attributes?

765