Hi all,
I am having a problem attempting questions on garbage collection. I think the way I approach to attempt the question is I am trying to draw some diagrams like...whenever I see an new reference variable, I am pointing to the object that it should be linked to. After that when the variable is reassigned to something else, thats where I fail to redraw. I know that I have to remove the arrow pointing to previous reference and now point to the newly assigned one.
Can somebody please help me understand how I should attempt GC questions...I am getting most of the questions wrong because of my approach. May be I still did not get the concept right.
Here is a simple example I have from whizlabs diagnostic exam.
How many object references will be refering to "Garbage" object at the end of the code.
So this is my approach
x---------> new Integer object 10
y---------> new Long object 100
z ---------> new
String object "Garbage"
obj ------> now pointing to the object refered by x (i.e) 10. At this point Integer obj 10 has 2 reference variables x,obj
x--------->z. That means x is now pointing to Garbage. At this point x's reference to 10 is removed and arrow is drawn to "Garbage".
z --------->y. That means z is now pointing to Long obj 100. At this point z's reference to "Garbage" is removed.
y---------->x. That means y is now pointing to Garbage. At this point string obj "Garbage" can be reached by two references x,y.
Please tell if my approach is correct. Should I follow the same approach for all GC questions.