1)method1(){ Integer[] y = new Integer[20];}
---->here only one object
or 21 objects get eiligible for garbage collection at the end of the method? When you create an array, all components are initialized to their default values (here null), so there is only one object, that is the array reference by y, that will be gced.
2)
Here after line #5 ,hello string get eligible for g.c, or never string literals get garbage collected? I'll let you search this forum to find an answer to this question.
3)What is the meaning of resurrecting an object in finalize() method?
could you explain with an example? When an object is about to be garbage collected, its finalize method will be invoked. In this method, your code could easily reassign a reference to the current object to some reference variable somewhere which would have to effect of resurrecting the object since a new variable is referencing it, and thus, it is not unreferenced anymore.
Example:
Output:
Before GC :highlander
Before GC :mortal
Finalize invoked for "highlander". Save "highlander"
Finalize invoked for "mortal". Don't save "mortal"
After GC :highlander
4)What is this weak reference and all? Actually you don't need to know that for the exam, but have a look at the following article which explains very well what WeakReference is and how garbage collection works:
http://developer.java.sun.com/developer/technicalArticles/ALT/RefObj You can also consult the API:
http://java.sun.com/j2se/1.4/docs/api/java/lang/ref/WeakReference.html [ March 21, 2002: Message edited by: Valentin Crettaz ]