Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B book question page 267

 
Ruth Lawrence
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ruth Lawrence
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there's a line of code missing - in the book, just before // doStuff it says:

c1 = null;


hth,

Bert
 
Ruth Lawrence
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Yours,
Bu.
 
Ruth Lawrence
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Bu. The code really helped.
Ruth
 
Amit Goyal
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Amit Goyal
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic