title: Immutability of Strings in Java
body: <p>Consider the following example.</p>
  
  <pre><code>String str = new String();
  
  str  = "Hello";
  System.out.println(str);  //Prints Hello
  
  str = "Help!";
  System.out.println(str);  //Prints Help!
  </code></pre>
  
  <p>Now, in Java, String objects are immutable. Then how come the object <code>str</code> can be assigned value "Help!". Isn't this contradicting the immutability of strings in Java? Can anybody please explain me the exact concept of immutability?</p>
  
  <p>Edit:</p>
  
  <p>Ok. I am now getting it, but just one follow-up question. What about the following code: </p>
  
  <pre><code>String str = "Mississippi"; 
  System.out.println(str); // prints Mississippi 
  
  str = str.replace("i", "!"); 
  System.out.println(str); // prints M!ss!ss!pp! 
  </code></pre>
  
  <p>Does this mean that two objects are created again ("Mississippi" and "M!ss!ss!pp!") and the reference <code>str</code> points to a different object after <code>replace()</code> method? </p>