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 GC 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 "GC" Watch "GC" New topic


hema janani
Ranch Hand

Joined: May 19, 2000
Posts: 70

Am I thinking logically-
please see the rational & confirm.
1 Consider the following code:
1. public void method(String s){
2. String a,b;
3. a = new String("Hello");
4. b = new String("Goodbye");
5. System.out.println(a + b);
6. a = null;
7. a = b; /****/what if it was b=a-then b is still refering/pointing to a ,will it then go to gc at line 6
,or object in a will never be gc.****???
8. System.out.println(a + b);
9. }

if I make no change in the code(ie. when a=b)- ans is c,-but if I make a change(ie b=a) as I have commented ans should be e.
please reply.

Where is it possible that the garbage collector will run the first time?

A. Just before line 5
B. Just before line 6
C. Just before line 7
D. Just before line 8
E. Never in this method

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
well, in both cases, GC CAN run just before line 7 FOR FIRST TIME. remember 'first time'. After line 6, "Hello" will be released, and it can be garbage collected.

I agree. Here's the link:
subject: GC
It's not a secret anymore!