Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Java implement String pooling? [duplicate]

Tags:

java

I want to understand the string pool more deeply. Please help me get to the source class file containing this implementation in Java.

The question is more of related to finding the source code or implementation of the String Pool to delve deeper on this concept to know more about some unknown or elusive things in it. This way we can make the use of strings even more efficiently or think of some other way to implement our own garbage collections in case we have an application creating so many literals and string objects.

like image 686
Ankit_ceo2 Avatar asked Feb 19 '16 06:02

Ankit_ceo2


People also ask

How is string pool implemented in Java?

String Pool in Java: Flow DiagramThe class is loaded when JVM is invoked. JVM looks for all the string literals in the program. First, it finds the variable s1 which refers to the literal “Apple” and it gets created in the memory. A reference for the literal “Apple” is then placed in the string constant pool memory.

How is string pool implemented?

As the name suggests, String Pool in java is a pool of Strings stored in Java Heap Memory. We know that String is a special class in java and we can create String objects using a new operator as well as providing values in double-quotes.

What is string pooling in Java?

String Pool in Java is a special storage space in Java Heap memory where string literals are stored. It is also known by the names - String Constant Pool or String Intern Pool. Whenever a string literal is created, the JVM first checks the String Constant Pool before creating a new String object corresponding to it.

Can we have duplicate values in string constant pool?

Although both reference variable s3 and s4 point to the String objects with the same value Hello but as these objects are creating with the new keyword, hence, these objects are stored in normal heap memory where the String objects with duplicate values can be stored.


2 Answers

I am sorry to disappoint you but the Java String-Pool is not an actual Java class but somewhere implemented in the JVM i.e. it is writen as C++ code. If you look at the source code of the String class (pretty much all the way down) you see that the intern() method is native. You will have to go through some JVM code to get more information.

Edit: Some implementation can be found here (C++ header, C++ implementation). Search for StringTable.

Edit2: As Holger pointed out in the comments, this is not a hard requirement of the JVM implementation. So it is possible to have a JVM that implements the String Pool differently, e.g. using an actual Java class. Though all commonly used JVMs I am aware of implement it in the JVMs C++ code.

like image 127
MartinS Avatar answered Oct 02 '22 17:10

MartinS


You can go through this article: Strings, Literally

When a .java file is compiled into a .class file, any String literals are noted in a special way, just as all constants are. When a class is loaded (note that loading happens prior to initialization), the JVM goes through the code for the class and looks for String literals. When it finds one, it checks to see if an equivalent String is already referenced from the heap. If not, it creates a String instance on the heap and stores a reference to that object in the constant table. Once a reference is made to that String object, any references to that String literal throughout your program are simply replaced with the reference to the object referenced from the String Literal Pool.

So, in the example shown above, there would be only one entry in the String Literal Pool, which would refer to a String object that contained the word "someString". Both of the local variables, one and two, would be assigned a reference to that single String object. You can see that this is true by looking at the output of the above program. While the equals() method checks to see if the String objects contain the same data ("someString"), the == operator, when used on objects, checks for referential equality - that means that it will return true if and only if the two reference variables refer to the exact same object. In such a case, the references are equal. From the above output, you can see that the local variables, one and two, not only refer to Strings that contain the same data, they refer to the same object.

like image 41
Rahul Tripathi Avatar answered Oct 02 '22 16:10

Rahul Tripathi