Hello all
Can someone tell me when will the
String object created will be Garbage
Collected.
1 class
Test 2 {
3 public static void main(String[] args)
4 {
5 String str = new String("Hello");
6 System.out.println(str);
7 }
8 }
I think after Line 6, It will be GC.
Does someone have differrent Answer.
Also look at the below code :-
This is from Bill Brodgen's Exam Cram - pg 163
1 class Test
2 {
3 public static void main(String[] args)
4 {
5for (int i = 10;i>=0;i--)
6{
7 String tmp = Integer.toString(i);
8System.out.println(tmp);
9 }
10 System.out.println("BOOM");
11 }
12 }
The Question asked is how many objects r GC after Line 10 ?
The Answer given was 10, & not 11 as it says that the local variable
still has a reference to the last String created.
What i don't understand is that in my first Example, if the object is
has no longer reference then it will be Eligible for GC which in the
example is at Line 6, i.e. after "System.out.println(str);"
So how come there is still a refernce in the Bill Brodgen's Example.
Thanks.
*/