Your C++ is showing.
There is no delete
in java, and all objects are created on the heap. The JVM has a garbage collector that relies on reference counts.
Once there are no more references to an object, it becomes available for collection by the garbage collector.
myObject = null
may not do it; for example:
Foo myObject = new Foo(); // 1 reference
Foo myOtherObject = myObject; // 2 references
myObject = null; // 1 reference
All this does is set the reference myObject
to null, it does not affect the object myObject
once pointed to except to simply decrement the reference count by 1. Since myOtherObject
still refers to that object, it is not yet available to be collected.