Register now or log in to join your professional community.
Core java basic question related to String class.
There are two conditions under which S will be garbage collected.
1. If S is assigned to null value during the course of application lifetime and after that S is never being reassigned to any other value.
2. When the application terminates JVM shutdown then S will be garbage collected if S is never being assigned to null value.
now s pointing to "xyz" object so run time "ABCD" removed by garbage collector
When the string "XYZ" will be assigned to s. the old s string object will be unreachable which means it is a garbage collected.
s he terminated when the program he not need him it s dynamically by jvm
when assign s="XYZ";
then reference of creating object of string by using 'new' keyword will be garbage collected
When the scope of this variable definition is reach to end
When there are no more references to that object or when the variable goes out of scope.
When the scope of the String s ends, or at the end of the program.
garbage collected will be "ABCD".