title: How is string pool implemented in java?
body: <p>I know it is very tempting to mark this question as duplicate/close vote, but please hear me out.</p>
  
  <p>I am trying to understand how string pooling works in Java.</p>
  
  <p>While there are dozens of answers here in StackOverflow on this same topic, no single answer seems to completely explain the implementation in details.<br>
  Information is fragmented over dozens of questions, each seem to point to some other answer as duplicate, with some answers even seeming to contradict others.</p>
  
  <p>The purpose of this question is to have one proper, complete answer to this question for posterity, rather than making them go over the cobweb of these QAs:</p>
  
  <p>(Purpose of this listing is also to indicate that I am aware of how many duplicate questions are here in this forum )</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/3801343/what-is-string-pool-in-java">What is String pool in Java?</a></li>
  <li><a href="https://stackoverflow.com/questions/2486191/what-is-the-java-string-pool-and-how-is-s-different-from-new-strings">What is the Java string pool and how is &quot;s&quot; different from new String(&quot;s&quot;)?</a></li>
  <li><a href="https://stackoverflow.com/questions/35498336/how-does-java-implement-string-pooling">How does Java implement String pooling?</a></li>
  <li><a href="https://stackoverflow.com/questions/4918399/where-does-javas-string-constant-pool-live-the-heap-or-the-stack/4918466#4918466">Where does Java&#39;s String constant pool live, the heap or the stack?</a></li>
  <li><a href="https://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext">What is the difference between &quot;text&quot; and new String(&quot;text&quot;)?</a></li>
  <li><a href="https://stackoverflow.com/questions/27210280/how-does-string-interning-work-in-java-7">How does string interning work in Java 7+?</a></li>
  <li><a href="https://stackoverflow.com/questions/27123131/underlying-mechanism-of-string-pooling-in-java">Underlying mechanism of String pooling in Java?</a>
  and many more ..</li>
  </ul>
  
  <p>(That there are so many duplicate questions, in fact , indicates that the answers to the existing questions are incomplete/unclear/not what people are exactly looking for)</p>
  
  <p>My question is actually similar to this question:<br>
  <a href="https://stackoverflow.com/questions/11700320/is-string-literal-pool-a-collection-of-references-to-the-string-object-or-a-col">Is String Literal Pool a collection of references to the String Object, Or a collection of Objects</a><br>
  The answer to the question is great btw, but talks nothing about String pool(!).</p>
  
  <p>I am confused because these seem to provide different explanations about the String Pool:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/a/4918466/2235839">This</a>, <a href="https://stackoverflow.com/a/23546082/2235839">this</a> answer seems to point to that the string pool is
  nothing but an area designated inside the heap memory specially for Strings</li>
  <li><a href="https://stackoverflow.com/a/35498395/2235839">This</a> answers says the same, but also talks about a constants table.    If that is correct, what is this constants table? How is
  it implemented (a HashTable?)What are its contents? What is its
  purpose?</li>
  <li>The OP in <a href="https://stackoverflow.com/q/11700320/2235839">this</a> question, as well as blogs like <a href="https://javaranch.com/journal/200409/Journal200409.jsp#a1" rel="nofollow noreferrer">this</a>, seems to indicate that String Pool is actually a "Constant Table", separate, and nothing to do with from heap memory</li>
  <li>Articles like <a href="https://javaranch.com/journal/200409/Journal200409.jsp#a1" rel="nofollow noreferrer">this</a> one indicates that there is a HaspMap involved. So is String pool actually a HashMap ( This seems very likely). If so what is the structure of the Map, as in, what are the keys and values, and how is it used by the JVM to implement the string pool?</li>
  </ol>
  
  <p>NOTE:<br>
  I am not looking for a answer that tells me the effects of String Pooling. For example, I am not looking for an explanation like this:</p>
  
  <pre><code>String s1 = new String("text"); //explicitly creates a new and referentially distinct instance of a String object; 
  String s2 = "text"; //may reuse an instance from the string constant pool if one is available.
  
  System.out.println(s1 == s2);      // false , not refering to same object   
  System.out.println(s1.equals(s2)); // true
  </code></pre>
  
  <p>I am trying t understand how String Pool is implemented by the JVM - what data structures it uses, how those data structures fit in the String pool logic, etc.</p>
  
  <p><strong>PS :</strong> Removed a line that goes with "Pardon me if I am being rude" from the question - frankly speaking that had more to do with my frustration than with my question, and was obviously in bad taste</p>