I've been reading Effective Java, and one thing that came out to me with the obsolete object reference item was his implementation of pop()
:
public Object pop(){
if (size == 0)
throw new EmptyStackException();
Object result = elements[--size];
elements[size] = null;
return result;
}
Why is it necessary to create a new reference to elements
? Why not do
elements[size] = null;
return elements[--size]
This nulls out the obsolete object reference without having to create a new recerence to the array.