Hello, How many Strings are eligable for Garbage Collection. Which String and when are they eligable? Thanks for your answers. String string1 = "Test"; String string2 = "Today"; string1 = null; string1 = string2; A) 1 B) 2 C) 3 D) 0
Quote: I say none also. Everything is still in scope. When the only reference to the "Test" String object was removed (changed to refer to null), scope had little to do with the situation. Were these regular objects, then the "Test" String object would indeed be eligible for garbage collection. As Ron alluded to, and as is pointed out in the conversation linked to by Anthony, the memory model used for Strings is a bit different than that used for other Java objects. [ September 20, 2002: Message edited by: Dirk Schreckmann ]
Yes the link is helpful. But I don't know if it actually clears up all my confusion.
Joined: Dec 10, 2001
William, If you'd like to get a better idea of how object referencing works, take a look at the nice flash animation that Corey McGlone put together. It can be accessed from http://www.geocities.com/mcglonec1978/javacert/javacert.html . Otherwise, if you have any specific (or vague) questions, just ask.
Yes that helped thanks. (It acts like a pointer. Even though Java doesn't have pointers.)
Joined: Jun 06, 2002
You can think of a reference variable in Java as a pointer. The difference between Java references and C/C++ pointers is that there is no such thing as pointer arithmetic in Java, and there is no "&" operator to generate pointers to local variables.