wood burning stoves 2.0*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC" Watch "GC" New topic
Author

GC

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
hema

Anonymous
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.

Ashish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC
 
Similar Threads
garbage collection
gc of String literals
GC
Question on Garbage Collection
Help.....Questions from Mock Exam