• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can anyone help

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At what point is the object anObj available for garbage collection.
01: public class Base{
02:
03: private void test() {
04:
05: if(true) {
06: String anObj = "sample";
07: String locObj = anObj;
08: anObj.trim();
09: anObj = null;
10: locObj.trim();
11: }
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18: }
Select most appropriate answer
a) After line 7
b) After line 8
c) After line 9
d) After line 10
e) After line 11
please help:
thank
Yaco
 
Balasubramanyam Kallavi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi:
I think the answer for your question is 'b' (After line 8). Immediately after line# 8, the 'anObj' reference has been assigned 'null', making the 'anObj' eligible for garbage collection.
Cheers,
Balu
 
paul wheaton
Trailboss
Pie
Posts: 21675
Firefox Browser IntelliJ IDE Java
 
Kumareswark
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't 'C' is the answer. Till line 9 object still alive only after that object looses its ref.
 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At what point is the object anObj available for garbage collection.
01: public class Base{
02:
03: private void test() {
04:
05: if(true) {
06: String anObj = "sample";
I think the question really means at what point is the object referenced by variable anObj can be GCed. at line 6, an object reference variable of Type String is assigned a new String object which contains "sample". till line 10 this object is still reachable by the locObj reference variable.so it is only after the locObj goes out of scope in line 11 that the original object can be GCed.
Correct me if I am wrong.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic