Suppose a string in Java is declared as String s = "hi"
I just want to know if the String object created above will ever be garbage collected?
if i write s = null, at this point will the String object be garbage collected?
Sommaire |
Suppose a string in Java is declared as String s = "hi"
I just want to know if the String object created above will ever be garbage collected?
if i write s = null, at this point will the String object be garbage collected?
Take it this way, the compiler will move it to the literal pool. Now all the instance of "hi" will be replaced by this value in the pool , which is actually a way compiler optimizes memory usage. No, these values are never garbage collected . However String object created at runtime would be garbage collected.
License : cc by-sa 3.0
http://stackoverflow.com/questions/12771533/garbage-collection-of-string-in-java