title: Memory management for Immutable Objects
body: <p>I have a conceptual doubt regarding the memory management of Immutable objects such as String Objects in java and other languages.  For example, if I have a String object "str" holding the value "Hello" and I do the following: </p>
  
  <pre><code>String str = "Hello";
  str = str.concatenate("World");
  </code></pre>
  
  <p>In this case, as I understand it a new String object with the state "Hello World" is created and referenced back to str. Now, in Java (and in most other object oriented languages as well) the lifetime of any object is as long as its reference is alive. So where does the object holding "Hello" go. Does it reside in the memory heap until a garbage collector takes care of it at its own leisure? Also, what about languages that do not support a garbage collector and have to depend on class destructors?</p>
  
  <p>Also, if mutable objects such as <code>StringBuffer</code> and <code>StringBuilder</code> are much more flexible and performance friendly, why make objects immutable in the first place, while designing the language??  (I mean why aren't String Objects mutable right from the beginning instead of having to introduce new structures such as String Buffers in subsequent JDK releases?).</p>
  
  <p>It would be great if someone can guide me on this. I am new to this so a clear, fundamental explanation would be highly appreciated. Thanks.</p>