• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Boone again

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI all,
This is from Boone's Mock Exam.
Can someone plz clarify that how the first is eligible for GC at label d.I feel args[0] is still referenced by riddle.
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:
}
}

options are
a)d:
b)e:
c)h:
d)i:
e)j:

OH! these GC always when I think I mastered them I goof up!!!
Thanx in advance.
Rajani
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This has been asked before. first is not pointing to args[0] but rather a completely different String that has the same contents as args[0].
Notice:
first = new String(args[0]);
and not
first = args[0];

------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
Mini Pilla
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry guys!! I got this at http://www.javaranch.com/ubb/Forum24/HTML/008400.html
 
reply
    Bookmark Topic Watch Topic
  • New Topic