As per Java,
There are two places in which strings are stored. String literal pool and heap memory according to its creation. I need to know, when assignment of string is done to another string, where does the newly created string will be stored?
I've done the assignment operations on both the type of Strings which are there in heap as well as in string pool. I got results like this.
String str = new String("foo"); ===> str is created in heap memory
String strL = "doo"; ===> strL is created in String pool.
But when,
String strNew = strL; // strL which is there in String Pool is assigned to strNew.
Now, If I do this
strL = null;
System.out.println(strNew)// output : "doo".
System.out.println(strL)// output : null.
Similarly,
String strNewH = str; // str which is there in heap is assigned to strNewH
Now,
str = null;
System.out.println(strNewH);// output : "foo".
System.out.println(str); // null
Above is the output I have got on IDE. According to this output, a new referenced object for strNew is created in string pool and a new referenced object for strNewH is created in heap. Is it correct?