aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP for Java 6, Garbage Collection question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP for Java 6, Garbage Collection question" Watch "SCJP for Java 6, Garbage Collection question" New topic
Author

SCJP for Java 6, Garbage Collection question

David G Harris
Greenhorn

Joined: Sep 23, 2009
Posts: 6
I have a question about Self-Test question #1, page 269 in the Sun Certified Programmer for Java 6 Study Guide.
I was wondering why Cardboard c3 is not eligible for garbage collection since is gets returned a null reference.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Please post the code here, since not all of us have the book in question


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
David G Harris
Greenhorn

Joined: Sep 23, 2009
Posts: 6
Here's the question and answer from the Self-Test

1. Given:



When //do stuff is reached, how many objects are eligible for GC [garbage collection]?

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

Answer:

C is correct. Only one CardBoard object (c1) is eligible, but it has an associated Short wrapper object that is also eligible.

**** I understand that c1 is eligible, but it looks like c3 should also be null and eligible. *****
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

c3 never pointed to any object. c3 points to null from the beginning...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
David G Harris
Greenhorn

Joined: Sep 23, 2009
Posts: 6
I think what you're saying is that since c3 was never allocated an address/reference, it isn't a candidate for garbage collection. Is that right?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

c3 is a reference in itself. What I want to say is that c3 never pointed to an object in the heap, so there is no question of the object pointed by it being eligible for garbage collection...
David G Harris
Greenhorn

Joined: Sep 23, 2009
Posts: 6
Got it, thanks!
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Question 1 : Line # 13 passing passing c2 object reference to Line # 4 and we are making it null at Line 5. So c2 also shold be null right because it is pas by reference ?
Questions 2 : What if we change Line 3 to short story = 200 ?
Question 3 : What if we change Line 3 to Short story; ?


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Harikrishna Gorrepati wrote:Question 1 : Line # 13 passing passing c2 object reference to Line # 4 and we are making it null at Line 5. So c2 also shold be null right because it is pas by reference ?

Why do you think so? There, one object with two reference variables (c2 and the method local variable cb). If you make cb as null, but the actual object is still can be access via c2. So, is it eligible?
Harikrishna Gorrepati wrote:
Questions 2 : What if we change Line 3 to short story = 200 ?

Then, primitives are objects? So do they?
Harikrishna Gorrepati wrote:
Question 3 : What if we change Line 3 to Short story; ?

What is the default value for instance object references, when you don't initialized?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
One of the reasons that GC is in the exam is so that candidates will get totally, completely, absolutely clear on objects vs. the variables that refer to objects. To reiterate from earlier threads, objects can be GCed, reference variables cannot.

With those hints in mind, can you guys get to a final resolution for the questions that haven't quite been answered in this thread?


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi Abimaran, I am not clear for Question 1. Could you please elaborate and explain in simple terms..
Primitives are not objects. So, short story = 200 doesn't relate/applicable to object.
Default value for object references (here, Short story;) is null, so story object is not created on the heap.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Harikrishna Gorrepati wrote:
Question 1 : Line # 13 passing passing c2 object reference to Line # 4 and we are making it null at Line 5. So c2 also shold be null right because it is pas by reference ?

Harikrishna Gorrepati wrote:Hi Abimaran, I am not clear for Question 1. Could you please elaborate and explain in simple terms..


In which point you are in trouble? Where is your problem? Did you Google about the Pass-By-Reference for Java?

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Yes. I know about Pass by reference and ran couple of examples.
My Question is Line # 13 passing passing c2 object reference to Line # 4 and we are making c2 object reference null at Line 5. So c2 also should be null because it is pass by reference ? Please correct me if I am going wrong.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Harikrishna Gorrepati wrote:
My Question is Line # 13 passing passing c2 object reference to Line # 4 and we are making c2 object reference null at Line 5. So c2 also should be null because it is pass by reference ? Please correct me if I am going wrong.


You copied the reference (the bit pattern) and pass it to another reference. So, noe two reference variable hold the same bit pattern for the object, which you've created in the 12th line.
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Yes. c2 hold same bit pattern. but we made it null at Line 5. So c2 at Line 12 also should be null know ??
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Harikrishna Gorrepati wrote:Yes. c2 hold same bit pattern. but we made it null at Line 5. So c2 at Line 12 also should be null know ??

No, it's just like this : You have an Identification Card (something like, student ID card/VISA) for you, and for a purpose, you need to give the exact copy of that one to a official purposes. But, there, they, that officials lost it. But, still you've another one. So, you can use it to be identify whenever you need to do.

[And think like, if you don't have any Identification Card, then the government will GCed you ]
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi Abimaran,

I am completely lost
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Harikrishna Gorrepati wrote:
I am completely lost


What??? Did you get it? Cleared?
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
No. If you don't mind, Can I call you (or) you can call me at [Phone Number Deleted, check this]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

Harikrishan, checkout this tutorial. It will help you understand why c2 is not eligible for GC...
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi Ankit, I have gone through this couple of times and can differentiate Pass by value and Pass by reference. Still I fee that, Line # 13 passing passing c2 object reference to Line # 4 and we are making c2 object reference null at Line 5. So c2 at Line 12 also should be null because it is pass by reference. If c2 was a primitive datatype, Line 12 wouldn't be null. Please advice
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

So c2 at Line 12 also should be null because it is pass by reference.

This is where you understood it wrong. There is no pass by reference in java. At line 13 when you call the go method, both c2 and cb start pointing to the same object, but when cb is made null, there is no effect on c2. You can read other tutorials if that tutorial didn't help you, like this one...
chirag visavadia
Greenhorn

Joined: Dec 21, 2009
Posts: 6

Harikrishna Gorrepati wrote:Hi Ankit, I have gone through this couple of times and can differentiate Pass by value and Pass by reference. Still I fee that, Line # 13 passing passing c2 object reference to Line # 4 and we are making c2 object reference null at Line 5. So c2 at Line 12 also should be null because it is pass by reference. If c2 was a primitive datatype, Line 12 wouldn't be null. Please advice


Harikrishna I was also in same thinking like you and I have try below program, you can run it and see that it is NOT pass by reference.


output:
before call: 7
go():i : 7
go():x : 4
go():i : 4
after call: 7


OCPJP6
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

David G Harris wrote:Here's the question and answer from the Self-Test


When //do stuff is reached, how many objects are eligible for GC [garbage collection]?

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

**** I understand that c1 is eligible, but it looks like c3 should also be null and eligible. *****


When at line 13, c2 is passed into the method, We're not passing the reference.. But actually, we're creating a copy of that reference(c2) and passing it to method..
Thus a copy of reference (c2) is created and cb is assigned to it..
So, now we have two different references pointing to the object initially pointed by c2.. i.e. c2 and cb
Now, even if we nullify the reference cb , c2 is still pointing to that object. So it will not be eligible for GC..

And as far as c3 is concerned.. I think you already got the answer for this in some previous posts..
I hope I made you clear..
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

i didn't read the whole post but i think the doubt is how java passes parameters . pass by value or pass by reference. the kb6 book clearly states that it creates a copy of the parameter and passes to the method. the book calls this PASS BY COPY OF VALUE.
 
jQuery in Action, 2nd edition
 
subject: SCJP for Java 6, Garbage Collection question