Weak Reference, Strong Reference - Java

Java provides two different types/classes of Reference Objectsstrong and weak; then, weak Reference Objects can be further divided into soft and phantom. Let's go per point.
Strong Reference Object
StringBuilder builder = new StringBuilder();
It's the default type/class of Reference Object, if not differently specified: builder is a strong Reference Object. This kind of reference makes the referenced object not eligible for GC, this means: whenever an object is referenced by a chain of strong Reference Objects, it cannot be garbage collected.
Weak Reference Object
WaekReference<StringBuilder> weakBuilder = new WeakReference<StringBuilder>(builder);
It is not the default type/class of Reference Object, in order to be used it should be explicitely specified like in the above example. This kind of reference makes the references object eligible for GC, this means: in case the only reference reachable for the StringBuilder object in memory is, actually, the weak reference, then the GC is allowed to garbage collect the StringBuilder object. When an object in memory is reachable only by weak Reference Objects, it becomes automatically eligible for GC.
Levels of Weakness
Two different levels of weakness can be enlisted: soft and phantom. A soft Reference Object is basically a weak Reference Object that remains in memory a bit more: normally, it resists GC cycle until memory is available and there is no risk of OutOfMemoryError (in that case, it can be removed). In the other hand, a phantom Reference Object is useful only to know exactly when an object has effectively removed from memory: normally they are used to fix weird finalize() revival/resurrection behaviors, since they actually do not return the object itself but only helps in keeping track of its memory presence (here a very comprehensive explanation).
Weak Reference Objects are ideal to implement Cache modules, in fact a sort of automatic eviction can be implemented by allowing the GC to clean up memory areas whenever objects/values are no longer reachable by strong references chain. An example is the WeakHashMap retaining weak keys.

Comments