Joe Lemmer wrote:I understand that String objects are immutable, and that modifying these Strings will mean that the references to them are lost and so they are discarded and lost in the String pool.
I'm not sure what you mean by "discarded and lost in the String pool".
If a string is in the string pool, which is so for string literals in your source code and strings on which you called
intern(), it will not be garbage collected, because the string pool is holding a reference to the string object.
String objects are not always in the string pool. A string object that's not in the string pool, and to which your program doesn't hold any references anymore, will be garbage collected.
Strings never get "lost in the string pool".