title: String Deduplication feature of Java 8
body: <p>Since <code>String</code> in Java (like other languages) consumes a lot of memory because each character consumes two bytes, Java 8 has introduced a new feature called <strong><em>String Deduplication</em></strong> which takes advantage of the fact that the char arrays are internal to strings and final, so the JVM can mess around with them.</p>
  
  <p>I have read <a href="https://blog.codecentric.de/en/2014/08/string-deduplication-new-feature-java-8-update-20-2/" rel="noreferrer">this example</a> so far but since I am not a pro java coder, I am having a hard time grasping the concept.</p>
  
  <p>Here is what it says,</p>
  
  <blockquote>
    <p>Various strategies for String Duplication have been considered, but
    the one implemented now follows the following approach: Whenever the
    garbage collector visits String objects it takes note of the char
    arrays. It takes their hash value and stores it alongside with a weak
    reference to the array. As soon as it finds another String which has
    the same hash code it compares them char by char. If they match as
    well, one String will be modified and point to the char array of the
    second String. The first char array then is no longer referenced
    anymore and can be garbage collected.</p>
    
    <p>This whole process of course brings some overhead, but is controlled
    by tight limits. For example if a string is not found to have
    duplicates for a while it will be no longer checked.</p>
  </blockquote>
  
  <p><strong>My First question,</strong></p>
  
  <p>There is still a lack of resources on this topic since it is recently added in Java 8 update 20, could anyone here share some practical examples on how it help in reducing the memory consumed by <code>String</code> in Java ?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>The above link says,</p>
  
  <blockquote>
    <p>As soon as it finds another String which has the same hash code it
    compares them char by char</p>
  </blockquote>
  
  <p><strong>My 2nd question,</strong></p>
  
  <p>If hash code of two <code>String</code> are same then the <code>Strings</code> are already the same, then why compare them <code>char</code> by <code>char</code> once it is found that the two <code>String</code> have same hash code ?</p>