posted 23 years ago
Objects are created on the heap. Garbage collection is done to keep the heap clean.
Variables are created in the stack. Each method has it's own stack. If it is a local variable, when the method completes, the stack for that method is discarded. The garbage collector does not do this. The variable s will be discarded whenever the variable goes out of scope.
However the String literal "XYZ" will get created at class load time in the Constant Pool, the garbage collector will not get rid of that.
"JavaRanch, where the deer and the Certified play" - David O'Meara