title: String Constant Pool
body: <p>As explained in these Stackoverflow questions: <a href="https://stackoverflow.com/questions/2009228/strings-are-objects-in-java-so-why-dont-we-use-new-to-create-them">question 1</a> &amp; <a href="https://stackoverflow.com/questions/1881922/questions-about-javas-string-pool">question 2</a> I understand that "<em>String literals</em>" are <a href="http://en.wikipedia.org/wiki/String_interning" rel="noreferrer">interned</a> when: </p>
  
  <pre><code>String s = "abc"; 
  </code></pre>
  
  <p>And that the JVM will create a new String object instead of using an existing one from the String Pool when:</p>
  
  <pre><code>String s = new String("abc");
  </code></pre>
  
  <p>However, I have a doubt after reading the following two similar statements.  </p>
  
  <ul>
  <li>From <a href="https://rads.stackoverflow.com/amzn/click/com/0071591060" rel="noreferrer" rel="nofollow noreferrer">SCJP preparation book</a>:</li>
  </ul>
  
  <blockquote>
    <p><em>When the compiler encounters a String literal, it checks the pool to see if an identical String already exists. If a match is found, the reference to the new literal is directed to the existing String, and no new String literal object is created.</em> </p>
  </blockquote>
  
  <ul>
  <li>From <a href="http://www.javaranch.com/journal/200409/Journal200409.jsp#a1" rel="noreferrer">JavaRanch:</a></li>
  </ul>
  
  <blockquote>
    <p><em>In this case, we actually end up with a slightly different behavior because of the keyword "new." In such a case, references to String literals are still put into the constant table (the String Literal Pool), but, when you come to the keyword "new," the JVM is obliged to create a new String object at run-time, rather than using the one from the constant table.</em></p>
  </blockquote>
  
  <p>So if we also put a reference in nonpool memory <strong>AND</strong> in pool memory when we create an object using "new" and based on the definitions above. <strong>Shouldn't the JVM also return the same reference when we do this?</strong>:</p>
  
  <pre><code>String one = new String("test");
  String two = "test";
  
  System.out.println(one.equals(two)); // true
  System.out.println(one == two);      // false
  </code></pre>
  
  <p>Because when declaring the String literal <code>String three = "test";</code> it will already exist in the pool? and therefore should return the same reference and print true? or do the previous statements mean that they will be put in pool memory but simply skipped when the <code>new</code> operator is used?</p>