• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

GC Stuff

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";
String newestName = name;
name = null;
//Line A
a) 0
b) 1
c) 2
d) 3
e) 4
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are missing something(A Question)
Alk
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. here u go
. How many objects are eligible for garbage collection once execution has reached the line labeled Line A?
String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";
String newestName = name;
name = null;
//Line A
a) 0
b) 1
c) 2
d) 3
e) 4
 
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's b.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's b as well - nothing is pointing to the "Nick" String, so that can be garbage collected, while the "Frieda" and "Jason" Strings still have references.
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's c (2). Isn't name eligible to be garbage collected because of the assignment name = null;? That would leave newestName and the second reference to newName intact assuming the first reference was wiped out by the second.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer is a)
In the given code, you have only string literals which are stored on strring literal pool but not on heap. Garbage collector wipes off only the objects which are stored on the heap but not on string (intern) pool.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here "Nick" is not referenced after line 2, since we are assigning new string "jason" to it.So, one object is eligible for garbage collection. But even if we are putting null to name, which has a value "Freida", "Freida" cannot be removed from string literal pool. Because string object of newestName is still "freida".
so, I think the answer is b.
Shyla Rajeev
 
Lucy C
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, actually I think ShivaPrasad is right - the Strings aren't in the program's space, so I don't think they're garbage collected at all. Might be a useful thing to remember for the exam - thanks! I'd have been right if all the Strings were created with a new String() statement though, wouldn't I?
Isn't name eligible to be garbage collected because of the assignment name = null;?
Betty, be careful not to mix up references and the objects they refer to! In the code example, the variable name first contains a reference to the String "Frieda". Then the value of name is assigned to newestName, which means that the newestName reference now refers to "Frieda" too. Finally name is set to null, so it doesn't refer to anything - but "Frieda" still has a live reference, newestName.
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the test, you won't be asked to guess about GC and string literals. Although String literals are used in the test, when it comes to GC, only Strings created with <CODE>new</CODE> are considered. And it may not even be Strings. It could be any object.
And don't be alarmed if you see the <CODE>append()</CODE> method used for the String class. This is such a crass error that Sun better correct soon. Assume they meant <CODE>concat()</CODE>.
 
Betty Reynolds
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Lucy for the clarification, and thanks Tony for the reassurance. After reading this discussion, I was starting to get concerned about how difficult (the subtleties) this exam might get. I know it's going to be tough, but I hope it's not full of "trick" questions.
 
Tony Alicea
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I don't think there are trick questions. I read that statement before in books, and I agree with it after taking the test.
One trick question that I saw in a mock test (which I promptly closed and never us again) was
Which are valid String declarations?
(a) String s = new String("ABC");
(b) String s = ("ABC");
etc...
Although those are valid questions, that type of trick question is not in the exam that I saw.
Hint: (b) is legal.
 
reply
    Bookmark Topic Watch Topic
  • New Topic