|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Learn About Java String Pool And `intern()` Method |
| 4 | +author: gaurav |
| 5 | +categories: [ Java, Core Java, String] |
| 6 | +description: String is an important class in Java. In this article I am explaining one of the most important concepts of String class i.e. the concept of Java String Pool and the `intern()` method. Read this article to understand this concept completely. |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +The string is an important class in Java. In this article I am explaining one of the most important concepts of String class i.e. the concept of Java String Pool and the `intern()` method. Read this article to understand this concept completely. I have given two code examples to make this concept easy to understand. |
| 11 | + |
| 12 | + |
| 13 | +## Introduction |
| 14 | + |
| 15 | +The string is a special class in java. Most of the programs in java use string class and its methods. Let’s see the String intern() method. |
| 16 | + |
| 17 | +When you are comparing strings, the one method you must know is the intern method of the string. So, you should know, when to use the `intern()` method, why should we use the `intern()` method and what will happen to the string after applying the `intern()` method to it. |
| 18 | + |
| 19 | +In this article, we will learn about the String intern() method and also the Java string pool concept. |
| 20 | + |
| 21 | +`intern()` method returns the canonical representation of the string. If you have got the previous sentence don’t be a worry. First of all, we will understand the string pool concept so that it will help you to understand the `intern()` method. |
| 22 | + |
| 23 | +So, let us learn about the Java string pool first. |
| 24 | + |
| 25 | +## Java String pool |
| 26 | + |
| 27 | +Java Virtual Machine (JVM) privately maintained a special memory space in the java heap memory. This special memory space is called a Java String pool. |
| 28 | + |
| 29 | +Java String pool is a reserved space to store unique string objects. |
| 30 | + |
| 31 | +Whenever we assign a string literal to string variable JVM checks if the string with the equal value is available in the string pool. |
| 32 | + |
| 33 | +If found, it returned the reference of its memory address to the string variable. |
| 34 | + |
| 35 | +If not found, that string will be added to the string pool and its reference will be returned. |
| 36 | + |
| 37 | +The above-mentioned process will happen automatically with string literals. The process of interning happens to the string literals automatically. See the note for more information. |
| 38 | + |
| 39 | +## `intern()` method. |
| 40 | + |
| 41 | +`intern()` method is used to add a unique copy of a string object in the string pool manually. |
| 42 | + |
| 43 | +When we create a string using the `new` operator the new object of the `String` is created in java heap space. |
| 44 | + |
| 45 | +As we apply the `intern()` method to that string variable, it first checks if the string with the equal value is available in the string pool. |
| 46 | + |
| 47 | +If found, it simply returns the reference of the memory address of that string object. |
| 48 | + |
| 49 | +And If not found, the copy of that string object will be added to the string pool and its reference will be returned. |
| 50 | + |
| 51 | +In both cases, the process seems like the same, but when we assign a string literal to the string variable, it will be interned automatically. When we create the string using `new` operator the string object will directly be added to the Java heap space. |
| 52 | + |
| 53 | +If we want to add that string object to the string pool, we have to apply the intern() method I.e. manually. |
| 54 | + |
| 55 | +``` |
| 56 | +Public String intern() |
| 57 | +Returns a canonical representation of a string. |
| 58 | +
|
| 59 | +Returns - |
| 60 | +It returns the string with the equal contents as this string but guaranteed from the string pool of unique strings. |
| 61 | +``` |
| 62 | + |
| 63 | +I have given two examples below. It will explain the interning of the strings in case of the string literals assigned to the string variable and also string objects created using the `new` operator. |
| 64 | + |
| 65 | +In the first example, we will see the automatic interning of the strings literals |
| 66 | + |
| 67 | +``` |
| 68 | +/** |
| 69 | + * A java program to compare the reference of the strings. |
| 70 | + * We use '==' operator to check the reference equality. |
| 71 | + * |
| 72 | + * @author Gaurav Kukade at coderolls.com |
| 73 | + * |
| 74 | + */ |
| 75 | +
|
| 76 | +public class StringReferenceCompare { |
| 77 | +
|
| 78 | + public static void main(String[] args) { |
| 79 | + |
| 80 | + String firstString = "coderolls"; |
| 81 | + |
| 82 | + String secondString = "coderolls"; |
| 83 | + |
| 84 | + System.out.println(firstString == secondString); //true |
| 85 | + } |
| 86 | +
|
| 87 | +} |
| 88 | +``` |
| 89 | +Output: |
| 90 | + |
| 91 | +``` |
| 92 | +true |
| 93 | +``` |
| 94 | + |
| 95 | +### Explanation: |
| 96 | + |
| 97 | +1. When we assign the “coderolls” value to the `firstString` variable the JVM checks if the string with the value “coderolls” is present in the string pool or not. |
| 98 | + |
| 99 | + JVM will not found a string with equal value in the java string pool because we have created it for the first time in this program. And therefore it will return the reference to that string after adding it to the string pool. |
| 100 | + |
| 101 | +2. When we assign the “coderolls” value to the `secondString` variable the JVM checks if the string with the value “coderolls” is present in the string pool or not. |
| 102 | + |
| 103 | + Since we have created the string will equal value in the first step of this program JVM will found it and as a result reference to its memory address will be returned. |
| 104 | + |
| 105 | +3. When we check both string using `==` operator it will print `true`, since both string variable is pointing towards the same string object. |
| 106 | + |
| 107 | +In the second example, we will see the manual interning of the string. |
| 108 | + |
| 109 | +``` |
| 110 | +/** |
| 111 | + * A java program to use an intern() method of the String class. |
| 112 | + * |
| 113 | + * intern() method returns the canonical representation of the string. |
| 114 | + * @author Gaurav Kukade at coderolls.com |
| 115 | + */ |
| 116 | +
|
| 117 | +public class UseInternMethod { |
| 118 | +
|
| 119 | + public static void main(String[] args) { |
| 120 | +
|
| 121 | + String firstString = "coderolls"; |
| 122 | + String secondString = new String("coderolls"); |
| 123 | + |
| 124 | + System.out.println(firstString == secondString); // false |
| 125 | + |
| 126 | + String thirdString = secondString.intern(); |
| 127 | + |
| 128 | + System.out.println(firstString == thirdString); // true |
| 129 | + } |
| 130 | +
|
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +Output: |
| 135 | + |
| 136 | +``` |
| 137 | +false |
| 138 | +true |
| 139 | +``` |
| 140 | + |
| 141 | +### Explanation: |
| 142 | + |
| 143 | +1. We have assigned the “coderolls” value to the `firstString` variable. JVM will checks if the string with the same value is present in the string pool. Since we have not created any string with this value JVM will add it to the string pool. |
| 144 | + |
| 145 | +2. We have created a string with the same value using the new operator and assigned it to the `secondString` variable. As a result, this string object will be stored in the java heap space. |
| 146 | + |
| 147 | +3. When we check both the strings using `==` operator, it will print the false since the `firstString` is pointing towards the string object in the string poo while the `secondString` is pointing towards the string object in the java heap space. Hence both `firstString` and `secondString` are pointing towards the different objects and it will print `false`. |
| 148 | + |
| 149 | +4. We have applied the `intern()` method to the `secondString` and assigned it to the new variable `thirdString`. |
| 150 | + |
| 151 | +5. After applying intern() method JVM will have a check if the string with same value as `secondString` is present in the string pool since it is already added in the first step ( when we have assigned “coderolls” to the `firstString`) JVM will found it and simply the reference to the memory address of it will be returned. |
| 152 | + |
| 153 | +6. Now, when we compare `firstString` and `thirdString` using `==` operator it will print `true` because both string variables are pointing towards the same string object in the java string pool. |
| 154 | + |
| 155 | +#### Note: |
| 156 | +All the literal strings and string-valued constant expressions are interned automatically. i.e.`firstString = "coderolls";` here `firstString` will be interned automatically. |
| 157 | + |
| 158 | +## Conclusion |
| 159 | + |
| 160 | +Java string pool is a special memory maintained by the JVM in heap memory to store a unique string object. For that reason. it will help in the code optimization and to reuse string objects. |
| 161 | + |
| 162 | +When you assigned a literal to string variable it will automatically be interned. This means the string object will be added to the java string pool automatically. |
| 163 | + |
| 164 | +In contrast, when you create a string using `new` operator you have applied `intern()` method to add its copy to the java string pool. |
| 165 | + |
| 166 | +If you found this article worth, please |
| 167 | + |
| 168 | +[Give me a cup of Coffee ☕](https://paypal.me/GauravKukade) |
| 169 | + |
| 170 | +If you have any queries about the code blocks given above, please write it down in the comment section below. Also. let me know if you have any other information about the Java String pool and `intern()` method in the comment section. |
| 171 | + |
| 172 | +### Related Article |
| 173 | + |
| 174 | +- [How do I compare strings in Java](https://coderolls.com/compare-strings-in-java) |
| 175 | +- [Reverse A String In Java (5 ways)](https://coderolls.com/reverse-a-string-in-java/) |
| 176 | +- [Compare Two Strings Lexicographically In Java](https://coderolls.com/compare-two-strings-lexicographically-in-java/) |
| 177 | +- [Introduction to Java Technology (Language and Platform)](https://coderolls.com/java-introduction/) |
0 commit comments