File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Bert Bates Kathy Sierra Java 6 Study Guide 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 "Bert Bates Kathy Sierra Java 6 Study Guide " Watch "Bert Bates Kathy Sierra Java 6 Study Guide " New topic
Author

Bert Bates Kathy Sierra Java 6 Study Guide

Paul Wam
Greenhorn

Joined: Apr 05, 2009
Posts: 20
Garbage Collection has me scratching my head a few times. Maybe you can explain - Chapter 3, question #10 reads




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

A. Three objects were created
B. Four objects were created
C. Five objects were created
D. Zero objects are eligible for GC
E. One objects are eligible for GC
F. Two objects are eligible for GC
G. Three objects are eligible for GC



The book says
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. When line 14 is reached, only the second Dozens object (and its "int array" object) are not reachable.


I thought B was correct (and not C) because for every new Dozens object, two objects are created (dozens and int array). There are two new Dozens objects (da[0] and d) thus 4 objects. I did not count da[1] because it points/refers to an object that already exists, d. Should I have...if so, why?
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
You are right about every new Dozens creating two new objects, a Dozen and an int array. Since 2 Dozens are created, you get 4 objects. PLUS: The Dozens array. It is also an object, making 5 objects.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

Paul Wam wrote:I thought B was correct (and not C) because for every new Dozens object, two objects are created (dozens and int array). There are two new Dozens objects (da[0] and d) thus 4 objects. I did not count da[1] because it points/refers to an object that already exists, d. Should I have...if so, why?

basically arrays are it self objects.
and when we say it means there are 2 objects are created, 1 object type of Dozens() 2nd object type of int array which Dozens class contains.
here it means you GCed 2 objects 1 the object of Dozens class itself and its int type of array.

HTH
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
M M kaimkhani wrote:
here it means you GCed 2 objects 1 the object of Dozens class itself and its int type of array.
HTH


Techincally, they just became eligible for GC at this point. We don't really know when (or even if) the GC will gather them before the end of the program.
Paul Wam
Greenhorn

Joined: Apr 05, 2009
Posts: 20
Thanks...I didn't catch that the array, da, is an object separate from the objects in its indexes or indeces.
Weiheng Zhang
Greenhorn

Joined: Jan 23, 2010
Posts: 9




[Thumbnail for gc illustration.gif]


- SCJP SCWCD
j dam
Greenhorn

Joined: Feb 01, 2010
Posts: 9
i hope this helps.


[Thumbnail for exm.jpg]

Mehdi Ben Larbi
Ranch Hand

Joined: Aug 17, 2010
Posts: 70
I am sorry,but i didn't understand :

Why isn't the d[1] eligible for GC,it's null reference too ?


SCJP 93 % ,preparing for SCWCD
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Mehdi Ben Larbi wrote:I am sorry,but i didn't understand :

Why isn't the d[1] eligible for GC,it's null reference too ?

d[1] is a reference, not an object. So how is it eligible for GC?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Mehdi Ben Larbi
Ranch Hand

Joined: Aug 17, 2010
Posts: 70
Abimaran Kugathasan wrote:
d[1] is a reference, not an object. So how is it eligible for GC?


So references to Array Objects ( d[0],d[1] ... ) are never eligible for GC ?
I am really confused with GC,i am having a lot of trouble understanding it.
Do you have any good study material for the exam please ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bert Bates Kathy Sierra Java 6 Study Guide