• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Number of objects in Array

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
George please Quote Your Sources when you post a question...
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic