• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

SCJP for Java 6, Garbage Collection question

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post the code here, since not all of us have the book in question
 
David G Harris
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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. *****
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
c3 never pointed to any object. c3 points to null from the beginning...
 
David G Harris
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it, thanks!
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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; ?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abimaran,

I am completely lost
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:
I am completely lost



What??? Did you get it? Cleared?
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. If you don't mind, Can I call you (or) you can call me at [Phone Number Deleted, check this]
 
Ankit Garg
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Harikrishan, checkout this tutorial. It will help you understand why c2 is not eligible for GC...
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Greenhorn
Posts: 7
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a question I couldn't find anywhere regarding the instance variables of a Class.

I was wondering why is elegible for Garbage Collector. I was wondering, are all instance variables elegible for garbage collector once the main method ends? and if not, what are the exceptions? I would like to know.  Thanks,

Regards
 
reply
    Bookmark Topic Watch Topic
  • New Topic