title: Behaviour of weakhashmap with String literal and String object
body: <p>I am understanding the concept of WeakhashMap. String literal and String object made it difficult to understand.</p>
  
  <p>Following is the code:</p>
  
  <pre><code>package com.lnt.StringBuf;
  
  import java.util.HashMap;
  import java.util.Map;
  import java.util.WeakHashMap;
  
  public class Test1 {
      public static void main(String[] args) {
  
          Map w = new WeakHashMap();
          Map h = new HashMap&lt;&gt;();
  
          String hkey = new String("hashkey");
          String wkey = new String("weakkey");
      /*  String hkey = "hashkey";
          String wkey = "weakkey";*/
  
          h.put(hkey, 1);
          w.put(wkey, 1);
  
          System.gc();
  
          System.out.println("Before");
          System.out.println("hashmap size: " + h.size());
          System.out.println("weakmap size: " + w.size());
          System.out.println("Hashmap value: " + h.get("hashkey") + "\t"
                  + "weakmap value: " + w.get("weakkey"));
  
          hkey = null;
          wkey = null;
  
          System.gc();
          System.out.println(hkey+" "+wkey);
  
          System.out.println("After");
          System.out.println("hashmap size: " + h.size());
          System.out.println("weakmap size: " + w.size());
          System.out.println("Hashmap value: " + h.get("hashkey") + "\t"
                  + "weakmap value: " + w.get("weakkey"));
  
          System.out.println(h.entrySet());
          System.out.println(w.entrySet());
  
      }
  
  }
  </code></pre>
  
  <p>Output is:</p>
  
  <pre><code>Before
  hashmap size: 1
  weakmap size: 1
  Hashmap value: 1    weakmap value: 1
  null null
  After
  hashmap size: 1
  weakmap size: 0
  Hashmap value: 1    weakmap value: null
  [hashkey=1]
  []
  </code></pre>
  
  <p>But when 
  String hkey = new String("hashkey");
  String wkey = new String("weakkey");</p>
  
  <p>is replaced with following code, output changes.</p>
  
  <pre><code>String hkey = "hashkey";
  String wkey = "weakkey";
  </code></pre>
  
  <p>Output is:</p>
  
  <pre><code>Before
  hashmap size: 1
  weakmap size: 1
  Hashmap value: 1    weakmap value: 1
  null null
  After
  hashmap size: 1
  weakmap size: 1
  Hashmap value: 1    weakmap value: 1
  [hashkey=1]
  [weakkey=1]
  </code></pre>
  
  <p>Question: Making String literal and String object 'null' impacts in different way in WeakHashMap. What is the reason?</p>