This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC question" Watch "GC question" New topic
Author

GC question

howie jao
Greenhorn

Joined: Nov 08, 2009
Posts: 14
Given:
class Dozens{
int[] dz = {1,2,3,4,5,6,7,8,9,10,11,12};
}
public class Eggs{
public static void main(String[] args){
Dozens [] da = new Dozens[3];
da[0] = new Dozens();
Dozens d = new Dozens();
da[1] = d;
d = null;
da[1] = null;

In above code, Five object were created and two object are eligible for GC.
Dozens [] da = new Dozens[3] < -- create one object: da array
da[0] = new Dozens() <--- this line create two object: Dozens and dz array
Dozens d = new Dozens() <-- this line create two object: Dozens and dz array
d = null <-- Dozens object and its dz array are eligible for GC.

Is my intepretation correct for number of object created and object for GC? I just want to make sure that I am understanding the code correctly/



Question 2:
class Beta{}
class Apha{
static Beta b1;
Beta b2;
}
public class Tester{
public static void main(String[] args){
Beta b1 = new Beta(); Beta b2 = new Beta();
Alpha a1 = new Alpha(); Alpha a2 = new Alpha();
a1.b1 = b1;
a1.b2 = b1;
a2.b2 = b2;
a1 = null; b1 = null; b2=null
}}

only one object is eligible for GC , why?

Thanks for any help
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

howie please Quote Your Sources when you post a mock exam question...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
howie jao
Greenhorn

Joined: Nov 08, 2009
Posts: 14
Hi, sorry
it is from Sierra and Bates book
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40


Please search the ranch first -- those two questions have been asked many times before (note: the search link is near the top of this page).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC question