• 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

K&B SCJP6 Chapter 3- Q10 a GC question

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand why the answer of this question is "Five obejects were create" and "Two objects are eligible for GC". I agree with there is total five objects were created, but at the line 14, I think there are three objects are eligible for GC. My explaination are follow the code:



at line8, da create a array and has 3 Dozens objects:
da[0] --> "Dozen1"
da[1] --> "Dozen2"
da[2] --> "Dozen3"
(so far created 3 Objects)

Line9, da[0] --> another "Dozen4", give up its object "Dozen1"
(so far, created 4 Objects, 1 eligible for GC)

Line10, d --> new "Dozen5"
(so far created 5 Objects)

Line11, da[1] --> d's "Dozen5", give up its object "Dozen2"
(so far, created 5 Objects, 2 eligible for GC)

Line12, d --> null, give up its object "Dozen5"
(nothing change here, due to the d's object is pointed by da[1])

Line13, da[1] --> null, also give up the object "Dozen5"
(so far, still 5 Objects, but 3 are eligible for GC)

What is wrong with my understanding? why the answer is 2 Object are eligible to GC

Thanks for help

[ September 17, 2008: Message edited by: Doraemon Zhou ]
[ September 17, 2008: Message edited by: Doraemon Zhou ]
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I also feel the answer should be "three objects" eligible for GC.

If I am wrong please correct me.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Doraemon Zhou :
at line8, da create a array and has 3 Dozens objects



First of all there are some compilation issues in the code. If we ignore that and talk about the real issue

When you say :


only one object will be created (Array object of type Dozen) When you initialized it as



Two objects (one Dozen object and one array object refered by dz inside the Dozen class) will be created here.
Same with the


So altogether 5 objects.

When you say


object reffered by da[1] with it's array object (dz) is eligible for the garbage collector.


object refered by d won't be eligible since da[0] refernces that object.

Hope this is clear
 
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
The answer is correct. Two objects are eligible for garbage collection.

1st object is created at line 8,
2nd object is created at line 9, along with it an int array is created. That's the 3rd object created
4th object is created at line 10, along with it an int array is created. That's the 5th object created

The 2 objects created at line 10 are eligible for garbage collection after line 13...
 
Maggie Zhou
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, Now I know I misunderstood the array part. THanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic