Sunday, July 15, 2007

Soft references

If an object is not referenced by by a strong reference and there is at least one link to that object with a soft reference, then the object is a softly referenced object. Java provides us a class, which is a subclass of java.lang.ref.Reference, called SoftReference. Soft references are created by encapsulating the object which we want to refer, inside a SoftReference object. Some examples of soft references are shown below.

SoftReference sf = new SoftReference(new String(“abc”));


SoftReference sf = new SoftReference(new WeakReference(new String(“abc”)));

String s = new String(“abc”);

SoftReference sf = new SoftReference(s);

WeakReference wr = new WeakReference(s);

s = null;

In these examples we see another type of reference called a weak reference. The next section talks about weak references.

Note: This text was originally posted on my earlier blog at

1 comment:

Joshua Smith said...
This comment has been removed by a blog administrator.