Originally posted by jignesh soni:
So how do you know when next GC is and my other part is unanswered yet. I am not clear wheen we concat two strings, third string will be created and which one of these three strings will not have reference, probably newly created concatted string right ?
if you write the code as in your original post:
String str1 = "1st string";
String str2 = "concat to 1st string";
str1.concat(str2);
then yes, str1 still refers to "1st string", str2 still refers to "concat to 1st string", and there is no reference to the newly created string "1st stringconcat to 1st string". This newly created string is ELIGIBLE for garbage collection, but that may never happen... or it may happen right away. there is no way to know.
HOWEVER, if you change your code to this:
String str1 = "1st string";
String str2 = "concat to 1st string";
String str3 = str1.concat(str2);
after this code runs, you will HAVE a reference to the newly created string, and can use it later in your program.