1 Answer

0 votes
by

As the name suggests, String Pool 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 object using new operator as well as providing values in double quotes.

How to create a string?
To create a String object in Java, there are two ways:
Using the new operator. For example,
1
String s1 = new String("Joey");
Using a string literal or constant expression. For example,
1
2
String s1="Joey"; (string literal) or
String s1="Joe" + "y"; (string constant expression)
Now, what is this String pool that I am talking about and how is the creation of a string in Java related to this. Let me cut down the clutter!
What is String Pool in Java?
String Pool is a storage area in Java heap.
String allocation, like all object allocation, proves to be a costly affair in both the cases of time and memory. The JVM performs some steps while initializing string literals to increase performance and decrease memory overhead. To decrease the number of String objects created in the JVM, the String class keeps a pool of strings.
Each time a string literal is created, the JVM checks the string literal pool first. If the string already exists in the string pool, a reference to the pooled instance returns. If the string does not exist in the pool, a new String object initializes and is placed in the pool.
...