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 a) 0 b) 1 c) 2 d) 3 e) 4 I would say 3 objects that is "Nick" after it was reassigned to Frieds. Then Frieda, after it was reassigned to null and newest name, since reassigning it to name and then to null. Am i right
Let me say something about this type of question. The string objects in the example should be String objects created with the <CODE>new</CODE> keyword. That's because String literals like plain <CODE>String s = "ABC"</CODE> are not treated the same. Their treatment is beyond the scope of the intended question. (But I see that Paul already said so!) For the purposes of the exam, I am sure that String objects, in a question about GC, will be always '<CODE>new</CODE>' Strings. Having said that, let's re-write the example:
String name; String newName = new String("Nick"); newName = new String("Jason"); name = new String("Frieda"); String newestName = name; name = null; //Line A a) 0 b) 1 c) 2 d) 3 e) 4 I say 1 (b).
Tony Alicea Senior Java Web Application Developer, SCPJ2, SCWCD
Hi Tony, Congrats on becoming the moderator for this group. I have already bookmarked the main discussion page, but how do i know if some of the posts which i have made have a new reply or not. In javacert, since individual messages were there, it was easy to know. Also Marcus's new discussion site has a facility to check only the last day's message. How do we do it here? Coming back to the question, how can you say only one object will be eligibe for GC (what about the assignment of name to Newestname, i think i am wrong here, but is damm confused). Also if i set Newname and Newestname to null, how many objects will be eligible for GC. Sorry for the trouble, tony (this is a simple case, but it damm confusing) and thanking you in advance.
When you get to the main page for the Saloon, there is link called "Click Here To View Today's Active Topics (all public forums)" Does this do what you want? Plus, here, when you are registred, the software leaves a cookie on your browser so it can track when you were last here. It will then show you all of the new stuff since you were last here with either lit lightbulbs (the forums page) or with pink folders (a forum page). Not adequate? As for Tony's new and improved question - his answer is correct. "what about the assignment of name to Newestname": newestName is a new reference that references the same thing that name does. So there is no new gc opportunity on this line. When name is set to null, newestName is still pointing to the object than name was pointing at, so there is still no opportunity for new gc. "if i set Newname and Newestname to null, how many objects will be eligible for GC": 3. Or "all of the strings that were created."
Hi Paul, Thanks for the reply. I know about the light bulbs in the top page. What i was referring is that once inside the programmer certification page, how do i know whether any new message have been added or not and is there any way to see only the new messages that have been added (something like marcus's site). That is really good. This is also good, but the only limitation is that i will have to go toall the topics and browse down to see if there is any new message. Even in javacert.com, by just seeing the topic, i was able to decide whether to read it or not, which is not possible here. Please note that this is just a request.
Could you hold this kitten for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, the permaculture playing cards