here i have a confusion about String instantiation .
Let suppose I have a code fragment like bellow
String st=new String("abc");
when I use a == operator for str and str1(str==str1) String object I will get true. Which means both the reference pointing to a same String object.
But when I use a == operator between str and st (str==st) then it will come as false (in spite there hash code being same . ) which means they are pointing to different object
which should not be as far as String pool is concerned. So is the third object is going to the heap rather than a String pool.
please help me regarding this String pool and heap concept.
then it makes no sense for Java to make four different String objects. Strings are immutable (the content of a String object cannot be changed after it has been created). So, to save memory, Java will create just one String object, in the string pool, and it will reuse it whenever you use the same string literal. In the example above, all four variables will refer to the same String object.
The purpose of the string pool is to make it possible to do this sharing. It's a memory usage optimization.