How many objects are eligible for garbage collection once execution has reached the line labeled Line A? String name; String newName = "Nick"; newName = "Jason"; name = "Frieda"; String newestName = name; name = null; //Line A
Hi theius/mahesh, answer is zero. because all the objects are String literals which will be lying in String pool. and literals in String pool are nod gc-ed during the life time of your program. regards deekasha
Hi Deekasha, Can you please elaborate more on this. Now I'm really confused. I'm giving my JCP next week. And all this while, I thought string objects are like any other objects and are ELIGIBLE for GC when they don't have any references. Thanks in advance Vasanth
Hello all, The answer is 1 object will be available for Gc. i.e the object "name". See this. String name; String newName = "Nick"; newName = "Jason"; name = "Frieda"; String newestName = name; name = null; // newestName here wil still be having its value as "Frieda" even after name = null
// A Hope this helps. Aruna.
[This message has been edited by Aru (edited August 23, 2000).]
thejus/mahesh, String objects ARE like other objects and they ARE eligible for garbage collection. But in the example you give, you did NOT declare any String objects, you made string literals. The difference is that a new String object is made with the new operator like this: String myString = new String("here's my string"); A string literal has direct assignment like this: String myLiteral = "I'm a string literal"; String literals go into the string pool. Each string literal only appears once in the pool. So if you made the declarations below: String lit_1 = "first"; String lit_2 = "first"; there will only be one addition to the string pool. I hope this helps. Good luck on your exam. Stephanie
Joined: Aug 21, 2000
hello deeksa.aru,vasanth and stephen thanx for the answer you gave ,but still i am not very clear please help me out? are string literals not gc? thanx in advance thejus/mahesh
I did not understand: �The difference is that a new String object is made with the new operator like this: String myString = new String("here's my string"); � That means that String name; <�> name = null; did not create a new object?
Joined: Aug 22, 2000
Hi all, I'm still not clear: 1. String s1=new String("hello"); 2. String s2="hello"; Statement 2 doesn't hv a new operator which means there is no string object, but still a string literal WHICH IS AN OBJECT* created in the pool. Now, will GC collect this object from this pool when I that object/literal has no references??? *correct me if I'm wrong.
Joined: Jun 14, 2000
If you guys are worried about garbage collection of String literals for the exam, I wouldn't worry. I really doubt they would ask you about garbage collection and String literals. Much more likely they will ask you about garbage collection of objects created with the new operator (which are eligible for garbage collection when there are no longer any valid references to them). But if you still want to know, this explanation was posted earlier and it seems very clear: Jim Yingst sheriff posted February 24, 2000 02:02 PM OK, this shouldn't come up on the exam, but I don't want this misinformation to continue to spread, so if you want to know about which kinds of strings are never collected, read on: Sree- that's a myth I have been trying to stamp out lately. Forget about whether the new operator was used - that isn't what determines whether a string can be collected. There are many ways to create new strings without a visible "new" operator, and almost all of them create an ordinary String which can be collected. The only exceptions are: 1.Strings which appear as literals in the code without any other operations being done to them, e.g.: String a = "literal"; 2.Strings which are the result of a constant-valued expression, e.g. String b = "not a " + "literal" + " but a constant"; or String c = "Pi equals " + Math.PI; (because Math.PI is final, it's considered constant) 3.Strings returned by the intern method, e.g. String d = anotherString.intern(); Cases 1 and 3 should be easy to identiry; case 2 is a little harder - you have to look at each part of an expression to determine if it's a constant, i.e. if it's a variable, it needs to be final. All the above examples are put in the intern pool, and can never be collected. All other kinds of strings are "normal" and can be collected (if there are no more references). So in this example: String s = "good" s+= "morning"; s = null; "good" is a literal and can never be collected, but "goodmorning" can be collected (s += "morning" is equivalent to s = s + "morning", and since s isn't a constant, the expression s + "morning" isn't a constant expression). [This message has been edited by Jim Yingst (edited February 24, 2000).] Hope this helps. Stephanie
Joined: Aug 21, 2000
hi everybody thansx for the answers now i am clear regds thejus/mahesh