title: Understanding Java's Reference classes: SoftReference, WeakReference, and PhantomReference
body: <p>Can someone explain the difference between the three Reference classes (or post a link to a nice explanation)? <code>SoftReference</code> > <code>WeakReference</code> > <code>PhantomReference</code>, but when would I use each one? Why is there a <code>WeakHashMap</code> but no <code>SoftHashMap</code> or <code>PhantomHashMap</code>?</p>
  
  <p>And if I use the following code...</p>
  
  <pre><code>WeakReference&lt;String&gt; ref = new WeakReference&lt;String&gt;("Hello!");
  if (ref != null) {                 // ref can get collected at any time...
      System.gc();                   // Let's assume ref gets collected here.
      System.out.println(ref.get()); // Now what?!
  }
  </code></pre>
  
  <p>...what happens? Do I have to check if <code>ref</code> is null before every statement (this is wrong, but what <em>should</em> I do)? Sorry for the rapid-fire questions, but I'm having trouble understanding these <code>Reference</code> classes... Thanks!</p>