• 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

OCA Java SE 8 Programmer I Exam Guide by K. Sierra and B. Bates - Chapter 6, Question 6, page 404

 
Ranch Hand
Posts: 74
2
Mac IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Given:



When I uncomment the block comment, I will see that every constructed object in Dozens[] da is "null"!

In this question are 7 answers:

Which two are true about the objects created within main(), and which are eligible for garbage collection when line 14 is reached?

A.  Three objects are created

B.  Four objects are created

C.  Five objects are created

D.  Zero objects are eligible for GC

E.  One object is eligible for GC

F.  Two objects are eligible for GC

G.  Three objectsare eligible for GC


The right answer in this book is:

C and F are correct. da refers to an object of type "Dozens array", and each Dozens object that is created comes with its own "int array" object (but not when it's null; my comment). When line 14 is reached, only the second Dozens object (and its "int array" object) are not reachable.

Answer F I can understand because two objects are eligible for GC: the second Dozens object and the int array object.

But why they see 5 objects that were created. I see only two da[0] and da[1] the others are null. When I write:



This three nulls that are created counts as objects?? When I read this article in Stackoverflow: https://stackoverflow.com/questions/1894149/is-null-an-object it is absolutely no object?!

Is the answer wrong in this book?

Thanks for help.

 
Mike Gualeni
Ranch Hand
Posts: 74
2
Mac IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw another similar thread :

Total number of objects would be 1 da object + 2 dz object + 2 Dozen object = 5

Now this explains why there are 5 objects.

I saw the similar threads only after posting my thread!


 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Mike,

always nasty questions, that OCAJP.

When a Dozens instance is created, also an int[] object is created. Thus: new Dozens() -> 2 new objects. And, likewise, if a Dozens instance has no references to it, then it can be gc, meaning 2 objects will be erased.

Now, in line 6 one object is created, Dozens[] da, and in lines 10 and 11 two Dozens instances are created. That makes 5 objects in total. Then the Dozens instance that is created in line 11, looses all references to it in lines 13 and 14.

And a teaser: what if the int[] dz in class Dozens was static?


Edit: I see you solved it yourself already. Well done!
 
Mike Gualeni
Ranch Hand
Posts: 74
2
Mac IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Piet,

Your answer was too fast that I could place my reply. But Thanks anyway, your post was very helpful.


About your teaser:

I think every created object of Dozens has the same int [] dz array, not as now every Dozens has its own int [] dz array. Right?
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right!

When the class Dozens loads, a static int[] is created. Every new instance of Dozens will now only create one object (that instance), and if a Dozens instance has no references to it, then only one object will be gc'd. So, how many objects are created now and eventually gc'd?      
 
Mike Gualeni
Ranch Hand
Posts: 74
2
Mac IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When Class Dozens is loaded the first time then one object int[] dz = {1,2,3,4,5,6,7,8,9,10,11,12} is created. And then da object is created (till now we've two objects), in line 10, 11 two Dozens objects are created, makes 4 objects and one object is gc'd. Right?

But another question:

Are the two lines 13, 14 necessary? It is not enough to mention only the line 13 ( d = null;) or line 14 ( da[1] = null; ? When d is null, we have on position da[1] a null reference? Or do I see a mistake, that only da[1]=null; sets the null reference in the array? But when d=null; then automatically da[1]=d; is null?!! Right or not?

Or do we set with d=null; only the variable d to null and in the array is still the old d reference Dozens d = new Dozens(); da[1]=d;?? And so we have also to mention line 14?

I know this question seems to be a little confusing but many thanks in advance.
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all: 4 objects created and one eligible, spot on!

The Dozens instance that is created in line 11, lives somewhere in memory. The variable d holds a reference to that Dozens instance. And with line 12, da[1] = d, da[1] now also holds a reference to the Dozens istance. That makes two. So, in order to make that Dozens thing eligible for gc, all references must be removed. And that means setting both d and da[1] to null. So, both lines 13 and 14 are necessary.

But don't take my word for it. Right after line 13 (d = null), print out da[1] and see whether it prints null, or a somewhat strange looking piece of text.
 
It's a beautiful day in this neighborhood - Fred Rogers. 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