1 class A{ 2 String s1="Hi"; 3 String s2="There"; 4 public static void main(String[] args){ 5 s2+=s1; 6 s1=null; 7 s1=s2; 8 System.out.println(s1); 9 } 10 } What is the earliest point where the memory space allocated to the string in line 2(s1) can be released. (a) before line 6 (b) before line 7 (c) before line 8 (d) before line 5
Basu Patel
Ranch Hand
Joined: May 28, 2000
Posts: 60
posted
0
To your question,
In line 6, s1=null; Here the old memory space s1="hi"; is released and s1 occupies a null value. Hope this helps! Basu
hello ken and basu, strings are created in string pool not in memory area. so even after s1= null statement there is no chance of garbage collected. "hi" string will still be in string pool.after some time if you write String s3 =" hi"; then s3 is referring that earlier "hi" which is in pool. gautam
eskay kumar
Ranch Hand
Joined: Jul 22, 2000
Posts: 71
posted
0
Hi, First of all the code will not compile as u cannot make static reference to the non static variables s1, s2 in main method. Assuming this is just a typo, i would like maha/ ajith/ paul/ tony/ jim to please help clarify my understanding of string pool and gc. I thought i understood it fine till i read a few posts here. 1. A new String is created in the foll code whether it exists in the string pool or not String s=new String("string"); 2. A new String is created if we use String s="newstring"; (A) irrespective of the fact that we don't use the new operator, IF the string "newstring" does not exist in the string pool. Now to the most imp point - a string pool exists for a class- so i am assuming that a new class with have an empty string pool in the beginning(or does it share it with classes in the same package - please explain) - so if a particular string has not been defined earlier as in case (A) it will be created. and we will start counting the references to it from this point. Now to determine when this string will be gc'd - we need to locate the point where no references to this string exist. And all these references will be apparent to us in the code . so where does foll point raised by sanjay fit in! <<<br /> strings are created in string pool not in memory area. so even after s1= null statement there is no chance of garbage collected.<br /> "hi" string will still be in string pool.after some time if you write<br /> String s3 =" hi";<br /> then s3 is referring that earlier "hi" which is in pool.>> Can u assume things like "hi" MAY be assigned to another variable at some point (which is not given in the current code)- so it can never be gc'd. This would mean be no string in the string pool can be gc'd, even if it has no references to it . BTW the ans to the main qn is (b) before line 7 according to me Kindly help eskay!
Sachin Kombrabail
Greenhorn
Joined: Aug 28, 2000
Posts: 14
posted
0
Hi All, To answer you question let me quote the JLS 1. Literal strings within the same class in the same package represent references to the same String object . 2. Literal strings within different classes in the same package represent references to the same String object. 3. Literal strings within different classes in different packages likewise represent references to the same String object. 4. Strings computed by constant expressions are computed at compile time and then treated as if they were literals. 5. Strings computed at run time are newly created and therefore distinct. 6. The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents. Now in our example there is no runtime allocations at all. The string literals "Hi" & "There" are created at compile time and are assigned. GC will only retrieve memory that has been allocated at runtime. One statement s2+=s1 does create a new string. But then it is never nulled. So i don't think that there is any scope for any GC in the program.