File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes gabage 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 "gabage collection" Watch "gabage collection" New topic

gabage collection

aruna ver

Joined: Jun 24, 2002
Posts: 8
in the following code

1. class myclass
3. public static void main(String [] args){
4. string s1 = "gabage"
5. string s2 = "collect"
6. string s3=s1+s2;
7. s1=null;
8. s1=s3
9. s2=s1;
What is the ealiest point when the memory allocated to s1 can be realsed is it line 7 or after line 9
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch.
Please notice that your displayed name doesn't comply with the naming policy. Please adjust to it.
Let's suppose the two variables were created with new. In that case the answer is 7, because so far the only refererence to the object was s1; and this reference is lost via the assignation of null.
In the exam you will not see a question about eligibility of string literals for g.c. You will only see "normal" objects. In fact the correct answer is that the objects computed from string literals are not normally g.ced

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: gabage collection
It's not a secret anymore!