Hi Fred & Loveen,
yes, you nulled 3 out of four elements of the array. And the objects that stood behind this array elements now are no longer referenced. So no living
thread can access them. And so they are garbage collected (or at least eligible for garbage collection).
The array itself is also an object.
You CAN change fields of an object (or null it), that is handed over as a parameter through a method. But you cannot directly null the object behind through this.
The individual elements of an array are like the the fields of any object. You can change or null this fields via the method, but not the object itself, here an example for an array:
prints:
before
[java.awt.Point[x=1,y=2], java.awt.Point[x=2,y=3]]
after deleteFirst()
[null, java.awt.Point[x=2,y=3]]
after noEffect()
[null, java.awt.Point[x=2,y=3]]
deleteFirst nulled the zeroth (0.) element of the array.
After nulling the reference in the noEffect method, the array is still there. The null in it is still from the deleteFirst method.
Fred said:
Maybe that is why array = null doesnt make any sense b'coz there is one MyClass object there.
As said above, you cannot null an object via a method parameter reference. Only the reference (anotherPointArray in Example2) will no longer point to pointArray from the main method, but it points to null then.
Not quite, as I said already. It makes no difference if the array is empty.
You can null the pointArray in the main method directly, even if it still contains elements. Then all of it's elements can no longer be referenced (reached) and will be eligible, as the array object itself.
Calling the asList method on it would then cause a null pointer exception.
If you'd just made an array of MyClass objects in main, fill it with MyClass object, and then null the array reference itself, all these elements will be eligible for GC. you can read the output from the finalize-method for them:
prints seven times: "method finalize" (but this is System dependent, as it is not guaranteed, that GC runs).
Yours,
Bu.