| Author |
Number of objects in Array
|
George Fung
Ranch Hand
Joined: Jun 12, 2003
Posts: 98
|
|
I think no of objects are 6
da is a array object
Under da, there are 3 Dozens objects
Line 9 an Line 10 creates one objects.
So, it totally creates 6 objects?
What do you think?
Given:
3. class Dozens {
4. int[] dz = {1,2,3,4,5,6,7,8,9,10,11,12};
5. }
6. public class Eggs {
7. public static void main(String[] args) {
8. Dozens [] da = new Dozens[3];
9. da[0] = new Dozens();
10. Dozens d = new Dozens();
11. da[1] = d;
12. d = null;
13. da[1] = null;
14. // do stuff
15. }
16. }
Which two are true about the objects created within main(), and eligible for garbage collection when line 14 is reached?
Three objects were created
Four objects were created
Five objects were created
Zero objects are eligible for GC
One object is eligible for GC
Two objects are eligible for GC
Three objects are eligible for GC
|
SCJP, SCJD, SCWCD, SCBCD, SCEA, SCJP6
To be obtained: SCEA 5
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
Dozens[] da = new Dozens[3] doesn't run the default constructor of the Dozens class. What it does is creates 1 array object on the heap. That array object has index 0,1 and 2. These can be used as references.
da -----> object[da[0] | da[1] | da[2]]
now da[0] ------> object(Dozen0 dz-------->object[dz[0]......dz[11]])
d------> object (Dozen1 dz-------->object[dz[0]......dz[11]])
Total number of objects would be 1 da object + 2 dz object + 2 Dozen object = 5
da[1] is assigned to d's object
now d and da[1] are nulled so their dozen object and the dz object referred from that object is eligible for gc. So 2 objects are eligible for gc.
gc problems can be best solved using a pen and paper.
|
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
|
 |
Alan Martinez
Greenhorn
Joined: Sep 17, 2009
Posts: 5
|
|
da[1] is two objects. But, what happened with the da object?? I said that three objects are reachable for the GC. 1 da object + 2 from 1st Dozens object created.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
George please Quote Your Sources when you post a question...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
As nitish said, when you create an array using new operator, then the elements in the array are assigned the default value for their types (0 for integers, false for boolean, null for references). So the statement Dozens[] da = new Dozens[3]; is equivalent to Dozens[] da = new Dozens[] {null, null, null} i.e. the elements da[0], da[1], da[2] are all null unless you assign them a value...
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Alan Martinez wrote:da[1] is two objects. But, what happened with the da object?? I said that three objects are reachable for the GC. 1 da object + 2 from 1st Dozens object created.
Hi Alan, welcome to javaranch.
da is never set to null itself (there is no statement saying da = null). Then why do you think that it will be eligible for GC??
|
 |
Alan Martinez
Greenhorn
Joined: Sep 17, 2009
Posts: 5
|
|
I understood the GC eligibility different. I believed that objects were eligible for GC, when they aren't null. =$
I'm a little confused on this topic.
Thanks Ankit!
|
 |
 |
|
|
subject: Number of objects in Array
|
|
|