making elements of an array null

arrays, java, null

Solution

A small snippet to show the difference:

// declare a array variable that can hold a reference.
String [] array;

// make it null, to indicate it does not refer anything.
array = null;

// at this point there is just a array var initialized to null but no actual array.

// now allocate an array.
array = new String[3];

// now make the individual array elements null..although they already are null.
for(int i=0;i<array.length;i++)
{
    array[i] = null;
}
// at this point we have a array variable, holding reference to an array, 
// whose individual elements are null.

Problem

are these two the same things? ``` for(int i=0; i<array.length; i++){ array[i] = null; } ``` and ``` array = null; ```

Original source