+ 3

Lost String Object

When we initialize a string by assigning it some value and then later when we try to manipulate the same string by concatenating or replacing we say that the new string is assigned to a newly created string object by the JVM but since we did not create a reference, it is lost. My question is, does garbage collector takes care of the lost objects? Or something else happens behind the scene. For example- String str = "hello"; str.concat("world"); What happens to the lost object which contains hello world.

10th Nov 2019, 7:55 AM
Avinesh
Avinesh - avatar
7 Respostas
+ 1
"Hello world" also exist in string pool until the garbage collection takes place.. i think.
10th Nov 2019, 1:21 PM
Jayakrishna 🇮🇳
+ 1
Yes. Garbage collection is takes place when no more variables point to that string, it is deallocated and eventually deleted from memory to free up space. and its automatic..
10th Nov 2019, 1:28 PM
Jayakrishna 🇮🇳
+ 1
Jaya krishna Denise Roßberg Thanks for helping.
10th Nov 2019, 1:43 PM
Avinesh
Avinesh - avatar
0
Jaya krishna So garbage collector clears that memory space?
10th Nov 2019, 1:23 PM
Avinesh
Avinesh - avatar
0
hhj
13th Nov 2019, 1:07 AM
Kassem Hafsi
Kassem Hafsi - avatar