Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC

 
hema janani
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic