Re: garbage collection
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
aidy schreef:
Hi,
I am studying for my SCJP and I have a question that I am unsure off.
"When will the string object created intially as arg1 be garbage
collected?
package com.tnt.august.eighth;
public class Myclass (
public static void main(String [] args) {
String msg;
String pre = "Trick ";
String mid = "or " ;
String post = "Treat. ';
String arg1 = new String (pre +
mid +
post);
msg = argl;
arg1 = null;
msg = pre + mid + msg;
pre = null;
mid = null;
System.out.println (msg);
msg = null;
post = null;
args = null;
}
}
The answer is: you cannot know, because garbage collection is
unpredictable. Only when the memory is full will it do its job.
Anyway, this code will not compile, it contains undeclared references
and syntax errors, so it is impossible to say.
H.
- --
Hendrik Maryns
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
iD8DBQFE2JOOe+7xMGD3itQRAvbyAJ0dep+rgSuKhxMdoit0VB33HTho6wCeOMQr
lm9Vavz+IVP8wHh4GAtcKks=
=QJZY
-----END PGP SIGNATURE-----