9

I have this bit of code for the Stack's pop method and i'm trying to figure out how it avoids loitering while still returning the element that our index is currently pointing to:

public String pop()
{ // Remove item from top of stack.
String item = a[--N];
a[N] = null; // Avoid loitering (see text).
if (N > 0 && N == a.length/4) resize(a.length/2);
return item;
}

From what i can understand we are pointing the reference item of our String object to the indexed element(we start from the last element using it's current size N-1 hence the decrements) of our array a. Then if we are returning the reference why are we setting that indexed element that our reference is pointing to null before doing so? Doesnt that make the item point to nothing and return nothing?

Kara
  • 6,115
  • 16
  • 50
  • 57
user2644819
  • 1,787
  • 7
  • 28
  • 41

2 Answers2

19

An object can't be garbage collected as long as it is reachable. If you simply change your index with --N but do not nullify a[N], you will keep a reference to that object, preventing its garbage collection even if the client code does not reference that object any longer.

That is one of the only cases where you need to nullify a variable in Java.

You also seem to misunderstand what references are. a[N] contains a value that points to an object in memory. When your write String item = a[N], you copy that value to the variable item. Both variables (item and a[N]) now refer to the same object. When you then write a[N] = null, you remove that reference from the array but item still contains a value that points to the original object.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • Ah right a is also a reference, but to the array object. I understand thank you. – user2644819 Aug 07 '13 at 17:45
  • So you are copying the reference of the array(the memory location of our object) to that of the item reference of our String object. So now item points to the array object. Setting our reference a to null is de-referencing it as we do not need it anymore. Question: does an array have multiple references like how C++ works with memory(each index points to a certain space in memory)? So essentially we are just de-referencing a certain part of it(so the garbage collector can collect)? – user2644819 Aug 07 '13 at 17:51
  • @user2644819 item does not point to the array object. It points to the same object as `a[N]` is pointing to. This post might make things clearer: http://stackoverflow.com/questions/40480/is-java-pass-by-reference – assylias Aug 07 '13 at 17:57
  • String[] a; a = new String[N]; So does the reference a point to a String object? So the reference item should point to that as well no? – user2644819 Aug 07 '13 at 18:08
  • 1
    in `String[] a = new String[N]`, `a` contains a reference that points to an object which happens to be an array. Each array item, like `a[0]` contains a reference that points to String. – assylias Aug 07 '13 at 18:37
  • How do we get rid of the `item` reference in this case then?! – razz Mar 29 '18 at 20:12
3

This copies the reference in the array. It doesn't reference the array member.

 String item = a[--N];

Now you have two references to the same object, one in the local variable, and one in the array. This removes the copy in the array:

 a[N] = null; // Avoid loitering (see text).

If it were not removed from the array, then an unnecessary reference would continue to exist, preventing garbage collection.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151