This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jan 30, 2000
Posts: 3222
    
    5
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.


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Garbage Collection
 
Similar Threads
Mock Quesion on Garbage Collection
Garbage collection
Garbage Collection-Strings
garbage collection
Garbage Collection