*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ExamLab Question on Garbage Collection. 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 "ExamLab Question on Garbage Collection." Watch "ExamLab Question on Garbage Collection." New topic
Author

ExamLab Question on Garbage Collection.

Thiyagarajan Kamala
Ranch Hand

Joined: Mar 16, 2003
Posts: 30


I believe the answer is zero i couldn't determine how when all the objects are set to null.
Balraj Momi
Ranch Hand

Joined: Jul 23, 2009
Posts: 45

Thats Correct, Answer is 0.

Here even though setNull() method is marking references to null, that is no way affecting reference inside caller method. Remember called method can only change values inside the passed reference type, It can not re-assign It to any new object or make it null.

Regards
Balraj Kumar
SCJP 5 95%
SCWCD 82%
Preparing for SCBCD
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

Thiyagarajan Kamala wrote:
I believe the answer is zero i couldn't determine how when all the objects are set to null.


You should know that objects are eligible for Garbage collection if know live thread can access them......

an object should atleast have one reference from stack to it(on heap)....

even if an objcet has many reference inside the heap but NOT EVEN A SINGLE REFERENCE FROM STACK, it is eligible for garbage collection only...


KARTHICK.C , SCJP6-93%
(Born to Win)
Jagat Sastry
Greenhorn

Joined: Feb 25, 2012
Posts: 3
At the cost of drawing flak for opening an ancient thread, I have one question regarding this.

Just before calling the setNull varargs method, isn't an array of Studio created before passing the object to the setNull method?
In that case, shouldn't the answer be "one"? (The array created just before setNull is called is eligible for garbage collection) Or does that come as an implementation detail?
ayush raj
Ranch Hand

Joined: Jan 15, 2012
Posts: 60
Balraj Momi
Remember called method can only change values inside the passed reference type, It can not re-assign It to any new object or make it null.


Please site some more example to get the concept clearer . I couldn't figure out why it happened to be zero as the output . As far as i knew , every reference variable being assigned null in setNull method , the two objects created are eligible for garbage collection .

Thanks

Ayush
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18510
    
  40

Jagat Sastry wrote:At the cost of drawing flak for opening an ancient thread, I have one question regarding this.

Just before calling the setNull varargs method, isn't an array of Studio created before passing the object to the setNull method?
In that case, shouldn't the answer be "one"? (The array created just before setNull is called is eligible for garbage collection) Or does that come as an implementation detail?



That's actually a good point -- the implementation detail of varargs does create an array that will be garbage collected. Not sure which way I stand on whether this is a bug in the question, but good catch nonetheless.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18510
    
  40

ayush raj wrote:
Balraj Momi
Remember called method can only change values inside the passed reference type, It can not re-assign It to any new object or make it null.


Please site some more example to get the concept clearer . I couldn't figure out why it happened to be zero as the output . As far as i knew , every reference variable being assigned null in setNull method , the two objects created are eligible for garbage collection .


It may be a good idea to review how Java passes parameters.

http://www.coderanch.com/how-to/java/CallByReferenceVsCallByValue

The parameters reference variables are local to the method -- and setting thoses references to null has no effect on the references in the main() method.

Henry
Alex Theedom
Ranch Hand

Joined: Jan 18, 2012
Posts: 48

An interesting point Jagat. I would imagine that an array is created by the vararg itself. I have no idea where I can go to check this other than to search the net. Which I did and I found a similar discussion on the stackoverflow site: http://stackoverflow.com/questions/7452513/java-varargs-allocates-array-objects. I would be interested in a definitive answer.


Alex Theedom - Senior Java EE Developer (OCPJP/OCPJWCD)
foxy milano
Greenhorn

Joined: Jul 03, 2011
Posts: 5
I think that it zero because: when the setNull method is called, it creates an array in the heap which has reference s on the stack. and all the element in the array (s[0] = s1 , s[1] = s2 , s[2] = s3 , s[3] = s4) are set to null. To conclude i think that when the method setNull terminate its execution, all the element in the array s are set to null, and all our assignment created in the main method are still valid. and the array create by setNull still has a reference to an array with null value element.
this is my point of view ...please check
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ExamLab Question on Garbage Collection.
 
Similar Threads
== with Strings is giving a different result
Doubt on Strings
is getting me confused
difference between == and equal() in sting
String and String Object Problem