I have refernced the following links
Garbage collection and Strings
http://www.xyzws.com/Javafaq/what-is-string-literal-pool/3
Questions about Java's String pool
Still have some doubts please help me
`public class StrPool
{
public static void main(String[] args)
{
String abc="hello";
String abcd="hello";
System.out.println(abc==abcd);
}
}
`
In the above example OP : true
So we can confirm that both object are refered from same String object Am clear about that.
`String abc="hello World";
String abcd="hello";
System.out.println(abc==abcd);`
This gives output : false
So String pool is carried out when two String object having same literal???
If So two object of String will created in String pool??
Why second output is false ???
I READ THAT String class is immutable
abc and abcd have different object reference then Immutable means
” First String object will created by JVM and give two reference to abc and abcd ” Am i right???
Thank you very much……..
The second output is
falseas both the stringabcandabcdhaving different content(text)yes as you said String class is immutable ie their content does not gets changed, if in case you change the string text then JVM will allocate a new space instead of altering it.
Now when you create a new
Stringreference with some text at that time JVM will check if that text already exists in pool and if then will refer your string to that text or else will create new text within pool