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 K&B book question page 267 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 book question page 267" Watch "K&B book question page 267" New topic
Author

K&B book question page 267

Ruth Lawrence
Greenhorn

Joined: Jan 24, 2007
Posts: 6
Hi everyone,

on page 267, the solution to question 2 states only the object refered to by c1 is eligible. But in the code, c2, passed in the method go(), is set to null too. Why isn't the object refered to by c2 eligible for GC?

Thanks.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Hi Ruth,

Welcome to the ranch!

I have a couple of requests:

1 - place update your display name to include your last name - it's one of two rules we have at JavaRanch.

2 - We encourage you to post and discuss mock exam questions, and we appreciate it when you give the source of the question as you have done, thanks! But, believe it or not, not everyone has a copy of the book So you should also post the entire question when you make your posts,

Thanks,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Ruth Lawrence
Greenhorn

Joined: Jan 24, 2007
Posts: 6
Oops, sorry! But thanks for the quick feedback.

The question is:

Given:

When // do stuff is reached, how many objects are eligible for GC?

The choices are:
A. 0
B. 1
C. 2
D. Compilation fails
E. It is not possible to know
F. An exception is thrown at runtime

The answer is C. Because only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.

My question is, when c2 is passed into go(), the object c2 refers to is set to null. Why isn't it also eligile for GC? If it is, would the correct answer be 4?

Thanks a lot.

Ruth

EDIT by mw: Added Code Tags.
[ February 01, 2007: Message edited by: marc weber ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I don't see that any objects are eligible.

A new CardBoard object (which has a Short object) is assigned to c1. Another new CardBoard object (which also has a Short object) is assigned to c2.

The object referenced by c1 is used to call the go method, but this does not affect c1's reference.

With the method call, a copy of c2's reference is assigned to the local variable cb. But because this is only a copy (a value), reassigning cb to null has no affect on c2, which still has its original reference to the same object.

The method returns a null reference, which is assigned to c3. But this variable never referenced an object to begin with, so nothing is made eligible.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
there's a line of code missing - in the book, just before // doStuff it says:

c1 = null;


hth,

Bert
Ruth Lawrence
Greenhorn

Joined: Jan 24, 2007
Posts: 6
My mistake again. Terribly sorry! Thanks to Bert for pointing it out.

But I did get the point, thanks to Marc. Even though we can modify the object when its reference variable is passed into a method, we can not reassign that variable to refer to another object. Thanks a lot.

Ruth
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
Yea, remember when you pass variable to methods you always pass copies.

if its a primitive value
-you pass a copy of the value

if its and object
-you pass a copy of the reference to that object


if its and enum .........?




















-you pass a copy to the reference to that enum


SCJP 5.0 95%<br /> <br />The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274


Yours,
Bu.


all events occur in real time
Ruth Lawrence
Greenhorn

Joined: Jan 24, 2007
Posts: 6
Thank you, Bu. The code really helped.
Ruth
Amit Goyal
Ranch Hand

Joined: Feb 21, 2006
Posts: 95



As the book says, the answer of this question is 2(means two objects are eligible for garbage collection).

I have a doubt, as the Short object is created on the object pool, even it is elegible for garbage collection.



Regards,

Amit Goyal

[ February 05, 2007: Message edited by: Amit Goyal ]

[ February 05, 2007: Message edited by: Amit Goyal ]
[ February 05, 2007: Message edited by: Amit Goyal ]
Kiran Gavate
Greenhorn

Joined: Jan 18, 2007
Posts: 25
Hi,
I too was studying the same chapter and needed clarification about exactly the same question. I came to the Javaranch and hey, my question is already being discussed here. Thanks for the explaination of the answer here, it's clear for me now.

But I have one more query. In the k&b that I have with me, the value of Short story = 5. In the errata, it has been corrected to 200. What difference would it make between these two values? Could somebody pls explain?


Kiran
Amit Goyal
Ranch Hand

Joined: Feb 21, 2006
Posts: 95
That make a lot of difference in java1.5, as if the value will be between
-128 to 127(for Byte,Short,Integer & Character) then its corresponding wrapper object will be retrieved from the java's object pool. So it will not be elegible for the garbage collection.


Regards,

Amit Goyal
Kiran Gavate
Greenhorn

Joined: Jan 18, 2007
Posts: 25
Thanks Amit,

Could you pls tell me where can I get more information on object pool and when the objects are stored in the object pool?
Amit Goyal
Ranch Hand

Joined: Feb 21, 2006
Posts: 95
This concept have been discussed inside the book "Sun Certified Java Programmer" by Kaithy Sierra & Bert Bates.

For more information you can refer JLS(Java Language Specification).

Regards,

Amit Goyal
Kiran Gavate
Greenhorn

Joined: Jan 18, 2007
Posts: 25
Hi Amit,

Thanks for your reply. Do you mean in chap 3 in the k&b book or somewhere else? I am only till chap 3 at this time. Pls let me know if this is explained in more details somewhere further.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B book question page 267