Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in garbage collection

 
Ravi Keshare
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I had Registered here two moth ago but this is my first post in this forum.

I m preparing for scjp 6 again. yes i had tried it once on Jan 29 (because my voucher is going to expire on jan 31) but failed (51%).

now i am going again for scjp and want to clear this time with 90+.

I had one doubt about the question in devka's diagnostic exam:




Question asked in the mock was "How many objects are eligible for GC after line 12 ?" .

And the answer is "1".

my doubt is that when creating object in the above question, null passed as argument to constructor .

will object be created on the heap (having "null" as value) this way ?

what i thought that there is no object created at all.........i was wrong but need explanation on this. please help.

and please forgive me for my grammatical mistakes......







 
Kieren Dixon
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try searching next time.
A thread about this question was posted only two days ago:
http://www.coderanch.com/t/442005/Programmer-Certification-SCJP/certification/many-object-eligible-garbage-collection
 
Jeena Jeen
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Kieren but the link is pointing to a different question. I also have the same doubt as Ravi that whether a new object will be created with a null reference or no object is created?
 
Amruth Puppala
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check if some one already raised in the forum when you send any question
 
Kieren Dixon
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, they looked very similar.

what i thought that there is no object created at all.........i was wrong but need explanation on this. please help.

You are correct, not wrong.
You have probably gotten something else wrong along the way and confused yourself.



So as you can see, there is only 1 object which does not have a reference
 
Ravi Keshare
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to you all for replying.

Kieren you explained with great efforts . what i got from it that whenever you say "new" an object is created on heap.
In above question "null" passed as argument to constructor, is actually assigned to an internal object
and this object is the property of an outer object .so outer object has a proper state which proves its existence.
please correct me if i am going wrong.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Keshare wrote: In above question "null" passed as argument to constructor, is actually assigned to an internal object
and this object is the property of an outer object .so outer object has a proper state which proves its existence.
please correct me if i am going wrong.

Yes, but don't use inner/outer object names, people may gets confused it with the inner/outer class. You can say like, "A class contains an instance of other(same) class" OR A HAS a A.
 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi i really am still unable to understand as

a1 ----> null

a2 -----> new A() ----> new A() ----> null meaning
a2 -------> null

a3 -------> new A ---> new A() -----> new A() ----> null
meaning
a3 --------> null

a1 = a3

a1.a = null here it should lead to NullPointerException coz a1.a is not reachable coz a1 ------> null

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic