When a String object is created using string literal (value enclosed in double quotes) that String object is created in a part of memory known as constant string pool in Java. In this tutorial you’ll learn what is string pool in Java and how it optimizes memory when Strings are created.
For another feature added to make String in Java more space efficient check this post- Compact Strings in Java 9
Java constant String pool
There are two ways to create a String in Java-
- Using String literal
- Using new keyword
When you create a String using String literal, for example
String str = “hello”
Memory for this string is created in a memory area knows as string pool in Java. This String pool is part of the heap memory.
Now the question is why this String pool is required and how it optimizes memory usage? String is a special class in Java and one of the most used too. That is why the concept of constant String pool is used to minimize memory usage.
Whenever any String literal is created JVM scans the String pool for any String having the same value, if found same reference is returned. So, new memory is not allocated for the same string, existing memory is used instead.
Let’s understand with an example. if two Strings are created as follows-
String str1 = “Hello”; String str2 = “Hello”;
Then the same memory is referenced by both the objects.
You can also verify it using a Java program. In the example two Strings are created using string literals and then their references are compared using equality '==' operator.
public class StringLiteral { public static void main(String[] args) { String str1 = "Hello"; String str2 = "Hello"; // checking if memory reference is same if(str1 == str2){ System.out.println("str1 and str2 are pointing to same memory reference"); }else{ System.out.println("str1 and str2 are not pointing to same memory reference"); } } }Output
str1 and str2 are pointing to same memory reference
This efficient usage of memory through String pool is possible only because String is immutable in Java.
Creating String using new operator
When a String instance is created using new operator, memory for the new instance is created on the heap rather than in the String pool.
For example if three Strings are created as follows-
String str1 = “Hello”; String str2 = “Hello”; String str3 = new String(“Hello”);
str1 and str2 share the same reference in the constant string pool where as str3 references to a memory location on the heap.
You can also verify it using a Java program. In the example three Strings are created, two using string literals and one using new operator. Their references are compared using equality ‘==’ operator.
public class StringLiteral { public static void main(String[] args) { String str1 = new String("Hello"); String str2 = "Hello"; String str3 = "Hello"; // instance and literal if(str1 == str2) { System.out.println("str1 and str2 are pointing to same memory reference"); }else{ System.out.println("str1 and str2 are not pointing to same memory reference"); } // instance and literal if(str1 == str3) { System.out.println("str1 and str3 are pointing to same memory reference"); }else{ System.out.println("str1 and str3 are not pointing to same memory reference"); } // literal and literal if(str2 == str3) { System.out.println("str2 and str3 are pointing to same memory reference"); }else{ System.out.println("str2 and str3 are not pointing to same memory reference"); } } }Output
str1 and str2 are not pointing to same memory reference str1 and str3 are not pointing to same memory reference str2 and str3 are pointing to same memory reference
If two instances having same value are created using new operator even then these two objects will be allocated different memory.
public class StringLiteral { public static void main(String[] args) { String str1 = new String("Hello"); String str2 = new String("Hello"); if(str1 == str2) { System.out.println("str1 and str2 are pointing to same memory reference"); }else{ System.out.println("str1 and str2 are not pointing to same memory reference"); } } }Output
str1 and str2 are not pointing to same memory reference
Java String interning
This process of sharing the memory is called interning in Java. In Java, string literals are "interned" so as to share unique instances, using the String.intern method. For String literals this process is implicit.
Explicitly using intern method
You can also intern a String by explicitly using intern method. That way you can even use the String pool for Strings created using new operator.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
public class StringLiteral { public static void main(String[] args) { String str1 = "Hello"; String str2 = new String("Hello"); // created using intern String str3 = new String("Hello").intern(); // str3 = str2.intern(); can be created this way too if(str1 == str2) { System.out.println("str1 and str2 are pointing to same memory reference"); }else{ System.out.println("str1 and str2 are not pointing to same memory reference"); } if(str1 == str3) { System.out.println("str1 and str3 are pointing to same memory reference"); }else{ System.out.println("str1 and str3 are not pointing to same memory reference"); } if(str2 == str3) { System.out.println("str2 and str3 are pointing to same memory reference"); }else{ System.out.println("str2 and str3 are not pointing to same memory reference"); } } }Output
str1 and str2 are not pointing to same memory reference str1 and str3 are pointing to same memory reference str2 and str3 are not pointing to same memory reference
As you can see String str3 is created using intern method so it will use the String pool. Since there is already a String with same value in the pool so str3 uses the same reference that is why str1 and str3 have the same reference.
That's all for the topic Constant String Pool in Java. If something is missing or you have something to share about the topic please write a comment.
You may also like
- Java StringBuffer With Method Examples
- Java StringJoiner Class With Method Examples
- Java String replace Method With Examples
- Java throws Clause With Examples
- ConcurrentLinkedQueue in Java With Examples
- Java Program to Find Common Element Between Two Arrays
- Insertion Sort Java Program
- Spring Bean Lifecycle Callback Methods
No comments:
Post a Comment