title: HashMap Object reference confusion
body: <p>Hello I'll go straight into the problem. I know that in HashMaps in java the valueSet stores REFERENCE, that means that if I change that reference the specific value of that key should change too since both refer to the same object.</p>
  
  <p>But I had some problems with my application so I tried to debug it to see how HashMap works practically. So I coded this mini proggram:</p>
  
  <pre><code>class Test
  {
      private final static HashMap&lt;Integer, String&gt; test = new HashMap&lt;&gt;();
  
      public static void main(String[] args)
      {
          String s = "String";
          test.put(1, s);
          test.put(2, "2");
          test.put(3, "3");
          s = "1";
          for (Integer i : test.keySet())
              System.out.println("Key: " + i + ", Value: " + test.get(i));
          }
      }
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>Key: 1, Value: String
  Key: 2, Value: 2
  Key: 3, Value: 3
  </code></pre>
  
  <p>Whats wrong here? I changed the value of String s right before Iterating the HashMap and it still shows the old value. That shows to me that HashMap DOES NOT work with object reference?</p>
  
  <p>Where am I wrong in here?</p>
  
  <p>Thanks for your time.</p>
  
  <p>EDIT:</p>
  
  <p>Tried with a test Object and the results are even more confusing since it seems like it WORKS for reference but not for Immutable objects as our fellow mate said:</p>
  
  <pre><code>class Test 
  {
      private final static HashMap&lt;Integer, TestObject&gt; test = new HashMap&lt;&gt;();
  
      public static void main(String[] args)
      {
          TestObject to = new TestObject();
          test.put(1, to);
          test.put(2, new TestObject());
          test.put(3, new TestObject());
          to.setValue(16);
          for (Integer i : test.keySet())
              System.out.println("Key: " + i + ", Value: " + test.get(i).getValue());
      }
  
      public static class TestObject
      {
          private int value;
  
          public void setValue(int val)
          {
              value = val;
          }
  
          public int getValue()
          {
              return value;
          }
      }
  }
  </code></pre>
  
  <p>this prints</p>
  
  <pre><code>Key: 1, Value: 16
  Key: 2, Value: 0
  Key: 3, Value: 0
  </code></pre>