String Pool

String Pool in java is a pool of Strings stored in Java Heap Memory.

String Pool is possible only because String is immutable in Java and it’s implementation of String interning concept.

What is String interning?

String interning is a method of storing only one copy of each distinct string value, which must be immutable. Interning strings makes some string processing tasks more time- or space-efficient at the cost of requiring more time when the string is created or interned. The distinct values are stored in a string intern pool.


String Pool in Java

String pool helps in saving a lot of space for Java Runtime although it takes more time to create the String.

When we use double quotes to create a String, it first looks for String with same value in the String pool, if found it just returns the reference else it creates a new String in the pool and then returns the reference.

However using new operator, we force String class to create a new String object in heap space. We can use intern() method to put it into the pool or refer to other String object from string pool having the same value.

String str = new String("Cat");

In the above statement, either 1 or 2 string will be created. If there is already a string literal “Cat” in the pool, then only one string “str” will be created in the pool. If there is no string literal “Cat” in the pool, then it will be first created in the pool and then in the heap space, so total 2 string objects will be created.

String Pool in Java, string pool, java string pool

  1. String s = new String(“xyz”);

The above line will create two object one is in heap and another is in String constant pool (SCP). The reference s will pointing to s always, and GC is not allowed in the SCP area, so all objects on SCP will be destroyed automatically at the time of JVM shutdown.

now if we do this

  1. String s = new String("xyz");
  2. String s1 ="xyz";

the above two statement will create two object. The first line String s = new String("xyz");will create two object as mentioned in 1st line and , When String s = "xyz";executes it checks in string constant pool if there is same content object is there or not, since the first line made an entry in string constant pool with “xyz” it returns the same reference and does not create other object.


Why String is immutable or final in Java

1.Requirement of String Pool : String pool is possible only because String is immutable in java, this way Java Runtime saves a lot of java heap space because different String variables can refer to same String variable in the pool. If String would not have been immutable, then String interning would not have been possible because if any variable would have changed the value, it would have been reflected to other variables also.

java-string-pool

2. Security : If String is not immutable then it would cause severe security threat to the application. For example, database username, password are passed as String to get database connection and in socket programming host and port details passed as String. Since String is immutable it’s value can’t be changed otherwise any hacker could change the referenced value to cause security issues in the application.

3. Immutable objects are naturally thread-safe: Since String is immutable, it is safe for multithreading and a single String instance can be shared across different threads. This avoid the usage of synchronization for thread safety, Strings are implicitly thread safe.

4. Caching Hashcode : Since String is immutable, its hashcode is cached at the time of creation and it doesn’t need to be calculated again. This makes it a great candidate for key in a Map and it’s processing is fast than other HashMap key objects. This is why String is mostly used Object as HashMap keys.


References:
https://www.journaldev.com/797/what-is-java-string-pool
http://www.kscodes.com/java/string-pool-in-java-string-initialization/
https://www.journaldev.com/802/string-immutable-final-java
https://stackoverflow.com/questions/19672427/string-s-new-stringxyz-how-many-objects-has-been-made-after-this-line-of
https://www.programcreek.com/2013/04/why-string-is-immutable-in-java/

Leave a comment