The following 2 examples in java explain the differences:
Example 1: Inaccessible Object
public void f() { Object obj = new Object(); obj.a(); obj = null;
In Example 1, there is no longer a pointer to the object we selected, so the obj object becomes inaccessible.
Example 2: dead object
public void g() { Object obj = new Object(); obj.a(); work();
In the second example, since we are not passing obj as a parameter and obj is not used in work (), the obj object is useless because it is not used anywhere. Please note that it is still available as it has a link. Since this is achievable, it cannot be garbage collected. Example 2 will have a memory leak and memory loss, even in Java with GC!
Sepideha
source share