• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC question

 
howie jao
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
howie please Quote Your Sources when you post a mock exam question...
 
howie jao
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, sorry
it is from Sierra and Bates book
 
Henry Wong
author
Marshal
Pie
Posts: 21417
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic