aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Gc 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 "Gc" Watch "Gc" New topic
Author

Gc

Angelika Angley
Ranch Hand

Joined: Feb 17, 2009
Posts: 40
After which line does the object created on line 1 is first eligible for GC?



I don't understand why the answer is 4 and not 2?
Local variable s in g() refers to the original object and if we set this reference to null, the original object without a referense is eligible for gc.


[Blog] [SCJP6 75%] [SCBCD]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Hi Angelika, welcome to javaranch.

I think this is a very basic question. I think this tutorial will clear your doubt...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Angelika Angley
Ranch Hand

Joined: Feb 17, 2009
Posts: 40
Is it a kind og joke?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

I am extremely sorry for my mistake. I was trying to post a tutorial link and I sent that image to my friend as a joke and posted the wrong link instead. I've rectified my mistake in the original post...
Angelika Angley
Ranch Hand

Joined: Feb 17, 2009
Posts: 40
Oh, I get it.
If we do something with a copy of the original reference, it doesn't affect the original reference itselv.
Apoorv Srivastava
Ranch Hand

Joined: Jan 03, 2009
Posts: 43
Its an-object-passing-to-a-method question.
See what is happening here...
We create a array of Strings(containing 2 Strings) and refer it with a reference variable named s.Now we pass this ref var to g() .As we all know in java everything is passed using CALL-BY-VALUE so the method g() makes its own local reference variable of type String [] and point it to the object created at line 1.Now inside g() the local reference variable s is assigned null but the s in the main() is still refering to the object created at line 1. g() returns the control to main().
Again the reference to object is passed to f() & f() creates its own local s and points it to the object created at line 1.Inside f() another String [] is created (having 4 Strings) and f()'s local s refers to this new String [] object. But again the object at line 1 is referred to by main() copy of s.f() returns to main() &
in main() a new String [] object is created and main()'s copy of s now refers to this newly created object.Since its here at line 4, that the object from line 1 is inaccessible to the code. Hence making it eligible for garbage collection...
Hope this explanation solved your problem.. & yeah go through the String tutorial in ranch journal..Its quite helpful.
:beerchug:


SCJP 1.5...Preparing for SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Gc