This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a number of labels in the source code below. These are labeled a through j. Which label identifies the earliest point
where, after that line has executed, the object referred to by the variable first may be garbage collected?

class Riddle {
public static void main(String[] args) {
String first, second;
String riddle;
if (args.length < 2)
return;
a: first = new String(args[0]);
b: second = new String(args[1]);
c: riddle = "When is a " + first;
d: first = null;
e: riddle += " like a " + second
+ "?";
f: second = null;
g: System.out.println(riddle);
h: args[0] = null;
i: args[1] = null;
j:
}
}
Select the one right answer.
a. d:
b. e:
c. h:
d. i:
e. j:
Answer is a. but in my opinion it should be b.
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Between lines (a) and (d) the only reference to the String created in (a) is variable <CODE>first</CODE>. So when it is set to null in line (d), the original object is eligible for GC.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic