String Pool Concept In Java

String In Java
The String object is one of the most used class in the Java language. String in Java are Objects that are backed internally by a char array. Since arrays are immutable, Strings are immutable as well. Whenever a change to a String is made, an entirely new String is created.
How To Create a String
In java there is two different methods to create a String object.
First method is using literal or constant expression :
String stringWithLiteral = "some Text"; //string literal
String stringWithConstant = "some " + "Text"; //constant expression
Second method is using “new” keyword :
String stringWithNew = "some Text";
Immutable String
Strings are constant objects in Java. Which means their value can not be changed after they are created. The String Pool can not be possible if String was not immutable in Java. There is some reasons for why Strings are immutable in Java.
- If the String was not immutable, it would pose a security threat. For example, usernames and passwords are passed as strings. String is immutable, so it’s value can not be changed after created. If String was not immutable, anyone can cause a security issue in the application by changing sensitive informations.
- The String is safe for multithreading because of its immutableness. Different threads can access a single “String instance”. It removes the synchronization for thread safety because we make strings thread-safe implicitly.
- String also used by JVM class loaders while loading classes. Immutability gives the security of loading the correct class by Classloader.
String Pool
String Pool is a storage area in heap memory. Object allocation is costly in both the cases of time and memory. The JVM performs some steps while initializing String to increase performance and decrease memory usage. To decrease the number of String objects created in the JVM, the String class keeps a pool of Strings.
String Interning
JVM can optimize the amount of memory allocated for Strings by storing only one copy of each literal String in the pool. This process is called String Interning.
When we create a String literal, the JVM searches the String Pool for a String with equal value. If the String already exists in the String Pool, a reference to the pooled instance returns. If the String does not exist in the String Pool, a new String object initializes and is placed in the String Pool.
How String Pool Works In Java
When we are creating a new String object with literal or constant expression, JVM checks if the same value exists in the String Pool.
String str1 = "Java"
String str2 = "Java"
If String Pool already contains this String literal, an instance of existing object is returned. If String Pool does not contain this String literal a new String is created and added to the String Pool.
If you want to change this behaviour, create a String using the new keyword :
String str3 = new String("Java")
With “new” keyword a new String instance is created even if it already in String Pool. Java provides a native method called intern. With this method we can add this literal to String Pool. This method also returns interned String.
String str4 = str3.intern()
Code Samples For String Pool
Before showing the example about String Pool, it should be reminded that the == operator in Java checks whether the addresses in memory are equal. So we will compare Strings using == operator to be sure that they are the same object or not.
String str1 = "Java";
String str2 = "Java";
String str3 = new String("Java");
String str4 = new String("Java").intern(); System.out.println(str1 == str2); // true
System.out.println(str1 == str3); // false
System.out.println(str1 == str4); // true
Thank you for reading my article about String Pool. I hope the contents elaborated here helped you in widening your knowledge base.