title: Reference to which String object is returned from the pool when calling intern()
body: <p>The Java Language Specification states that </p>
  
  <blockquote>
    <p>When the intern method is invoked, if the pool already contains a
    string equal to this String object as determined by the
    equals(java.lang.Object) method, then the string from the pool is
    returned</p>
  </blockquote>
  
  <p>In the following code snippet: </p>
  
  <pre><code>class StringPoolTest {
    public static void main(String[] args) { 
  
      String first = "string";
      String second = new String("string");
      String third = "string".intern();
  
      System.out.println(System.identityHashCode(first));
      System.out.println(System.identityHashCode(second));
      System.out.println(System.identityHashCode(third));
    }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>989184670
  268130470
  989184670
  </code></pre>
  
  <p>I added a String object <em>first</em> to the pool (assigning a String literal to the reference) and explicitly created <em>second</em> using a String constructor.
  There are now <strong>two</strong> identical character sequences in the pool.</p>
  
  <p>I wanted to see which one would be returned when calling <em>intern</em>. Since the method <em>hashCode</em> is overridden for the class String, I used <em>System.identityHashCode</em> to see exactly which two String references were the same.</p>
  
  <p>Clearly, <em>intern</em> returned the reference to the object created using a String literal. Why is this so? Are there any rules regarding which reference is returned in the case of multiple identical String objects in the pool?</p>