dear fellow,
u must differtiate between a variable or variable name and an object that occupying space. GC one and only key rule is
"An object(occuping space) gets eligble for garbage collection when it could not be accessed again or any refrence to it has be lost or destroyed"
in your both question 'anObj' is variable name not the object where as "sample" is the real obj so untill u have a refrence to it, it can not be garbage collected
07: anObj.trim(); as "sample" have nothing to trim so no new obj get created nor u r dislocating the anObj so an obj is still pointing the smae "sample"
09:anObj=anObj.toUpperCase();
as "sample" and "SAMPLE" is different objects which u can confirm by insering this line of code in your code
String anotherObj=anObj.toUpperCase();
if (anotherObj==anObj){
System.out.println("equal");
}
else{
System.out.println("unequal");
}
'unequal' get printed it means that new obj get created and its refrence has been assign to the anotherObj.
where as in your case 'anObj' is get assigned new obj refrence. so the old 'sample' is ready for garbage collection as it does not have any var to get refrenced in program.
in your next question the explanation is same what only matters is the local scope of 'anObj' and 'locObj' var after that 'sample' could not be refrenced so it is eligible for garbage collection just after 'if'
hope u not get confused
Nayyer