Maxim
Maxim

Reputation: 756

Is it necessary to set obsolete references null when they will get reassigned?

Let me first premise my question with the fact that I am learning programming/data structures. Yes, I know that Java has a collections API to do what I am coding, but for the moment, I would rather learn the basic ideas first.

I am coding an ArrayList implementation and am curious about garbage collection when references become obsolete. So in my case I have a generic object array i.e E[] data = (E[]) new Object[size]; and have a method E set(int index, E item) which will replace the data[index] with item. Here is my method body:

public E set(int index, E item) {
    if(index<0||index>size-1) throw new IndexOutOfBoundsException(); //Not relevant to question
    E previousValue = data[index];
    data[index]=null; 
    data[index]=item;
    return previousValue;
}

On to my question. Is it necessary to make the object null even if it the index is going to be reassigned. It seems like I should, but the code looks redundant to me. Or if am correct, is there a less redundant looking way to code this? I would really appreciate more than a single sentence answer, I'm really curious about this topic.

Upvotes: 1

Views: 633

Answers (2)

Alex
Alex

Reputation: 3013

No you do not need to set it to null. You can just overwrite the value. The JVM knows when a object has no more references to it. After that it is automatically garbage collected when the Garbage Collect is triggered.

How garbage collection works is best explained in the official documentation

The answer to which items are eligible for Garbage collection can be found on this SO post

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502606

No, there's no need to do that. It won't make any difference to garbage collection. It's only important to set a variable to null (in terms of garbage collection) in one of these cases:

  • It's a local variable in a long-running method
  • It's a static variable
  • It's an instance variable and the containing object itself is not going to be eligible for garbage collection

My personal experience is that these situations are relatively rare - for example, instance variables are usually useful throughout the lifetime of the containing object. Long-running methods containing some variables which aren't needed all the time should usually be split up into multiple methods, although that won't always be the case.

It's important to note that you're not setting an object to null - there's no such concept. You can only set the value of a variable to null. It's really important to distinguish between these concepts:

  • Variables (named storage locations)
  • References (a value which allows you to "navigate" to an object)
  • Objects

The value of a variable is never an object - only a reference or a primitive value. Think of a variable as being like a piece of paper - a piece of paper can't contain an actual house (the object) but you can write the address of a house on the piece of paper (the reference used to navigate to the object).

Upvotes: 10

Related Questions