1

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 )

(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:

  1. This, this answer seems to point to that the string pool is nothing but an area designated inside the heap memory specially for Strings
  2. 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?
  3. 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
  4. 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

A Nice Guy
  • 2,676
  • 4
  • 30
  • 54
  • What version of Java, and have you read through the source code of `java.lang.String`, and what **actual** problem are you trying to solve? – Elliott Frisch Nov 26 '17 at 16:45
  • Assume 7+,yes, but java pooling has to be implemented by JVM(https://stackoverflow.com/a/35498461/2235839),I am just trying to understand how String poling works – A Nice Guy Nov 26 '17 at 16:51
  • 1
    7+ includes Java 1.7, 1.8 and 1.9 (and all the builds); and Oracle and IBM. The answer you linked to includes all of the relevant source for the OpenJDK 8 (including C++). Read it again. – Elliott Frisch Nov 26 '17 at 16:57
  • considering that from java 1.8, there has been changes in jvm structure answers may differ for version 1.7 and 1.8+ – Ankit Nov 26 '17 at 17:53
  • 1
    It's worth asking - what problem will this help you solve? If it's just curiosity, obviously that's fine :). But in that case the canonical place to find answers to questions about how it's implemented under the hood is to look under the hood - the JVM source code. – Oliver Charlesworth Nov 26 '17 at 18:04
  • 1
    The JVM requires literal strings with the same value to be `==`. This can be implemented in any fashion the designer chooses. For the Hotspot JVM, there is a class called `StringTable`, which is an internal hash table type. See http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/sun/jvm/hotspot/memory/StringTable.java#StringTable . For a bit more info see http://blog.jamesdbloom.com/JVMInternals.html#string_table. – Gene Nov 26 '17 at 19:53
  • For any normal java developer, this is an implementation detail only. It has no effect on your code. For advanced or framework/lib developers, it's recommended you go through the relevant sections of the java language spec anyway. – Agoston Horvath Nov 26 '17 at 20:34
  • Aren't these sorts of things covered in the spec? – Dexygen Nov 26 '17 at 22:26
  • Oh I will go through the specs/documentations, yes I will. But I have a job, and I can dig through these stuff only in my spare time.I was hoping if there is anyone in this community that already knows the concept, he/she would spare his 10 mins of time to help me out.That would save me hours, probably days worth of time.Am I asking for too much? Isn't that what communities are for - sharing knowledge? – A Nice Guy Nov 27 '17 at 06:54
  • 1
    "Here are 7 duplicates to the same question, but I didnt like them so Ill ask it for the 8th time" – vikingsteve Nov 27 '17 at 08:22
  • 1
    If it wouldn't be a duplicate, your question would probably be too broad. – Mark Rotteveel Nov 27 '17 at 08:27
  • @MarkRotteveel : Yes probably it will be.Nvm. – A Nice Guy Nov 27 '17 at 09:13
  • @vikingsteve : I thought I explained why I mentioned the duplicates in the question.Its not that I didn't like ( whats there to like/not like anyway,what is, is). Its just that many answers seem to contradict each other.I want to know whcic one is correct, or if all of them are missing something. Again, if someone already knows, otherwise dont bother :) – A Nice Guy Nov 27 '17 at 09:17
  • @Arnab You don't need to know anything about the *implementation*, and the less you know about it the better, as it can change any time. What you need to know is the *specification*, and [that is already answered seriously well in the duplicate](https://stackoverflow.com/a/43423384/207421). – user207421 Nov 27 '17 at 09:21
  • @EJP : Thanks for atleast taking my question seriously. Yes it doesnt matter implementation wise. I was just wondering that's all.And then when I started to look in Stack Overflow, i saw that other people have similar confusion.(https://stackoverflow.com/questions/11700320/is-string-literal-pool-a-collection-of-references-to-the-string-object-or-a-col). So I thought I will collate similar questions in one place, and try to explain where/why I am confused. Thats all :) – A Nice Guy Nov 27 '17 at 09:25
  • What you're really confused about is asking about the implementation instead of the specification. The specification is already documented, by Sun and Oracle, without any assistance from StackOverflow. Creating yet more duplicates of duplicates here is not an improvement. You should rely on official specifications, not arbitrary Internet resources like SO. – user207421 Nov 27 '17 at 09:47

0 Answers0