aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B SCJP6 Chapter 3- Q10 a GC question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B SCJP6 Chapter 3- Q10 a GC question" Watch "K&B SCJP6 Chapter 3- Q10 a GC question" New topic
Author

K&B SCJP6 Chapter 3- Q10 a GC question

Maggie Zhou
Greenhorn

Joined: Sep 09, 2008
Posts: 26
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 ]

Preparing SCJP 6, try to get high score, and looking for a job...
Swathi Kota
Ranch Hand

Joined: Jun 04, 2008
Posts: 52
Yes I also feel the answer should be "three objects" eligible for GC.

If I am wrong please correct me.


SCJP 6, SCWCD 5
Success is how high you bounce when you hit bottom
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

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


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Maggie Zhou
Greenhorn

Joined: Sep 09, 2008
Posts: 26
Thanks a lot, Now I know I misunderstood the array part. THanks
 
 
subject: K&B SCJP6 Chapter 3- Q10 a GC question