Object.hashCode() typically returns
the address of the heap block holding the objects data
Once an object is created,
Object.hashCode() must return
the same value each time it is called
But with compacting garbage collection,
an objects heap address can change
In that case,
the first time
Object.hashCode() is called,
it must save the hashcode somewhere
and return that each time
rather than returning
the objects current heap address
Finalization
If an object has a finalize() method,
the garbage collector calls it
before reclaiming an objects heap block
However, finalize() might resurrect the object
by storing a reference to it
in some other still-in-use object
So a Java garbage collector
typically needs an extra pass
to finalize purportedly garbage objects,
and only reclaim them if they stay dead
Java 2 Micro Edition Connected Limited Device Configuration
(J2ME CLDC)
avoids this complication
by omitting the finalize() method
Package java.lang.ref
Strong reference:
A normal Java object reference
Garbage collector will not reclaim an object
that has a strong reference
Soft reference:java.lang.ref.SoftReference
Garbage collector is allowed to reclaim an object
that has a soft reference
but no strong references
Can be used, for example, to create a cache of objects
that clears itself of no-longer-needed objects
if necessary to make room in the heap
Weak reference:java.lang.ref.WeakReference
Garbage collector must reclaim an object
that has a weak reference
but no soft or strong references
Can be used, for example, to create a data structure
that automatically deletes its own elements
when all other references to its elements go away
Phantom reference:java.lang.ref.PhantomReference
Garbage collector must reclaim an object
that has a phantom reference
but no weak, soft, or strong references
Can be used, for example, to do post-mortem actions
after an object is reclaimed
(the object itself cannot be accessed)
J2ME CLDC
avoids these complications
by omitting package java.lang.ref