Monday, February 4, 2019

java - Why does the specified object be eligible for garbage collection?

There's a difference between pass-references-by-value and pass-values-by-reference :)



Is Java Pass By Reference
Java is never pass by reference right right
Pass By Reference Or Pass By Value



You might want to check out Jon Skeet's article on C# parameter-passing semantics as well, seeing as it's his favorite 'programmer ignorance' pet peeve:
What's your favorite 'programmer ignorance' pet peeve.



So basically, I see your code do the following:




c1 = new C("Alice");
// m1(C obj1) { -- c1 gets passed to m1, a copy of the reference is made.
// -- there are now two references to Alice (c1, obj1)
// obj1 = new C("Bob"); -- there is now one reference to Alice
// and one reference to Bob
// return obj1; -- returns a reference to Bob(c1 still reference Alice)
// } -- when m1 returns, one of the references to Alice disappears.
c2 = m1(c1); // c2 points to Bob
c3 = new C("Charlie");

c2 = c3; // <-- Bob is eligible for collection.
// There are now two references to Charlie

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...