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

What is oop in java?

733


What is difference between local variable and global variable?

731


what is thread in Java ?

919


How we can declare a static variable?

884


How do you sort in descending order in java using collections sort?

715


What do you mean by mnemonics?

774


When we serialize an object does the serialization mechanism saves its references too?

738


List two java ide’s?

824


How do you know if a value is nan?

808


What are the different collection views provided by maps?

778


What are alternatives to java serialization?

827


What is parsing a sentence?

771


What are three types of loops in java?

816


Why java is used everywhere?

774


Is string thread safe in java?

837