I know it is very tempting to mark this question as duplicate/close vote, but please hear me out.
I am trying to understand how string pooling works in Java.
While there are dozens of answers here in StackOverflow on this same topic, no single answer seems to completely explain the implementation in details.
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.
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:
(Purpose of this listing is also to indicate that I am aware of how many duplicate questions are here in this forum )
- What is String pool in Java?
- What is the Java string pool and how is "s" different from new String("s")?
- How does Java implement String pooling?
- Where does Java's String constant pool live, the heap or the stack?
- What is the difference between "text" and new String("text")?
- How does string interning work in Java 7+?
- Underlying mechanism of String pooling in Java? and many more ..
(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)
My question is actually similar to this question:
Is String Literal Pool a collection of references to the String Object, Or a collection of Objects
The answer to the question is great btw, but talks nothing about String pool(!).
I am confused because these seem to provide different explanations about the String Pool:
- This, this answer seems to point to that the string pool is nothing but an area designated inside the heap memory specially for Strings
- This 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?
- The OP in this question, as well as blogs like this, seems to indicate that String Pool is actually a "Constant Table", separate, and nothing to do with from heap memory
- Articles like this 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?
NOTE:
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:
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
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.
PS : 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