Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Gc

 
Angelika Angley
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angelika, welcome to javaranch.

I think this is a very basic question. I think this tutorial will clear your doubt...
 
Angelika Angley
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it a kind og joke?
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic