1 String s = "fasd"; 2 s = null; 3 String b ="afdas"; Which is the earlist line where string s is eligible for being garbage collected? Some books say line 2 where others say line 3. Which one is right?
Objects are marked for garbage as soon as they are inaccessable. Which translates that if Object o = someObject is nulled i.e. o = null; someObject will be marked for garbage if it is not referenced by any other reference(s). 1. null a reference 2. reference goes out of scope 3. reference is assigned to another object's instance
Well technically it will be marked after the execution of line 2. Not at line 2. So the wording of the question has to be accurate. Otherwise in this case it will be line 3.....
String s will never be eligible to the gc. Only objects created with keyword 'new' will be eligible to gc if their ref is set to null or there's no ref with them.