wood burning stoves 2.0*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

Alamu Vinai
Greenhorn

Joined: Feb 01, 2001
Posts: 19
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.
d:
e:
h:
i:
j:
There were no answer for this question in mock exam. Can anyone tell and explain the answer?
sai challa
Ranch Hand

Joined: Feb 06, 2001
Posts: 54
IMHO,
The object referred to by first gets garbage collected at line d).
first is the only string that is referring to it.At d) first is assigned a null reference and hence it no longer has the reference to the object to which it originally referred and hence the object gets garbage collected at step d).
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
(Be carefull: what you are about to read is NOT CORRECT
The variable first holds a reference to an object. Then it is used to create riddle. The riddle variable also gets a reference to the same object in line c. Setting first to null does not get rid of the reference in riddle, which is held until the end of the method.
At the end of the method all of the variables for the method are discarded so there are no more references to the object. At that time it is eligible for the gc. So the answer should be j.
(Cindy, Cindy, Cindy. . what were you thinking??? (hangs head in sadness) . . .)
[This message has been edited by Cindy Glass (edited March 09, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Lisa Yanchunis
Greenhorn

Joined: Nov 03, 2000
Posts: 27
Originally posted by Cindy Glass:
The variable first holds a reference to an object. Then it is used to create riddle. The riddle variable also gets a reference to the same object in line c. Setting first to null does not get rid of the reference in riddle, which is held until the end of the method.

Oh boy... i am confused now because I thought that sai was correct. Is not a NEW string object created when riddle is created (because of the "when is a" +). Strings are immutable (pounding this into my own head) so riddle cannot point to the same object as first but must point to a different object.
Help!
Lisa
Rajesh R
Greenhorn

Joined: Mar 02, 2001
Posts: 12
Garbage Collection always confuses me. But that does not stop me from sharing with you my understanding. Please correct me if I am wrong.
c: riddle = "when is a" + first
creates a new String object and the variable riddle holds a reference to this new object.
The assignment of null reference to variable first makes the object refered by first "eligible for Garbage Collection", but there is no guarantee that "which" thread will execute garbage collection and "when".
It could even be possible that garbage collection will not take place at all!(because the main method complete execution and JVM might exit!). However, if garbage collection takes place, it is not necessary that the object should be garbage collected in the same order it was made eligible for garbage collection. I could be garbage collected in any order!
I think this is why there was no answer for this question.
What do you think?
John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
i thought sai was correct as well.
on line "c", isn't a new string object created containing
the string "when is a ***" with the reference riddle
referring to it? (leaving the String referred to by "first"
just hanging around doing nothing?)
John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
i know this is a few days old but i'm still stumped on it.
"Which label identifies the earliest point where, after that line has executed, the object referred to by the variable first may be garbage collected?"

I know Cindy posted the (correct, i assume) answer but i still
don't get it. Something funny seems to be going in in line c.
Help..?
thanks.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Very interesting discussion. Here are my two cents -
Look at line c:

c: riddle = "when is a" + first

Here the result of concatenation operation is a new string. I strongly feel that the content of "first" string used to build the new string, not the reference itself.
Java internally uses StringBuffer for string object concatenation since they are mutable and easy to handle. Here is an extract from API documentation for java.lang.StringBuffer class -

String buffers are used by the compiler to implement the binary string concatenation operator . For example, the code:
x = "a" 4 "c"

is compiled to the equivalent of:

x = new StringBuffer().append("a").append(4).append("c").toString()

which creates a new string buffer (initially empty), appends the string representation of each operand to the string buffer in turn, and then converts the contents of the string buffer to a string. Overall, this avoids creating many temporary strings.

Clearly the new string created is not made of references to old string objects. ie., in our example "riddle" is not made of two reference pointers - one pointing to "when is a" and another refering to first. Instead "riddle" points to a whole new string "when is a first" and has nothing to do with our original "first" String object.
To test my argument simply print the "riddle" immediately after assigning "first" to null ie., just after line d. If indeed "riddle" was referring to "first", it should print "When is a (null) like a ...?". But that doesn't happen, which means "riddle" was a whole new object that was built using the content of the "first" variable, not the reference of the "first" variable.
After line "d" there are no active references pointing to the "first" variable and hence it is the earliest point at which the object referred to by "first" is eligible for garbage collection.

Hope this clarifies your doubts( if not ask for more )
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Siva Prasad
Ranch Hand

Joined: Feb 22, 2001
Posts: 104
Yes Exactly!
Ajith has given good explanation.
in line c riddle points to new string object which has been concatinated and the resulatant string is assinged to riddle
After this first is never used and immediately set to null at d:
For me its very clear now
Thanks Ajith
Siva
John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
thanks for the reply Ajith. great argument.
i didn't notice this until now but what about arg[0]?
even after line d:, it still thinks it's referring
to a string object. what if, after line d:, i want
to do something like System.out.println( args[0] ); ?
if the object referred to by first is garbage collected,
the system would've screwed me. correct?
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
John you're right. Even after line "d" the args[0] is still pointing to a valid string object. The reason is simple. Just like the new string that was created using the concatenation operator, the new String(args[0]) creates a whole new String object that has the content of args[0]. This means we now have two 'identical' string objects in memory. One is being pointed to by the reference "first" and the other one is being pointed to by "arg[0]".
Setting "first" to null in no way affects "args[0]" since they are two different objects. Try printing args[0] after line "d" and see what happens.
The point here is not to get confused between objects and references. An object can have any number of references. Some classes has overloaded constructors to help you achieve some kind of "copy construction". They are just convenience methods. However when you use new() keyword to create an object, Java creates a totally new object. The object created and the object that was used as an argument to the new() operator are two different objects eventhough their initial content may be the same.
I hope I have not confused you folks even more. The best way to master this concept, in my opinion is to write programs like this and play with it
Good luck,
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
whoops.
right you are!
thanks again Ajith. I've got it straight now.
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
So let me just be clear on this...
The common concensus here is that Cindy is incorrect, the answer should be d?


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
You would think, after all the times that I have explained that concatenating two Strings creates a new String object, SURELY I would not make that silly mistake. (Cindy slowly shakes head . . )
Just to be perfectly clear - Cindy was wrong. The answer should be d.
Perhaps it was early dementia??? Perhaps a long vacation is in order??? At any rate THANK YOU Ajith for clearing up the confusion that I caused.
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
Cindy
Have you being doing too much celebrating of your 1000th post?
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
You're welcome Cindy
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: garbage collection