Week 149 — What is String interning?

Question of the Week #149
What is String interning?
2 Replies
MrMisha | Coder
MrMisha | Coder2mo ago
When multiple parts of the code in an application contain the same String literal, the JDK uses a single object for all occurances of that literal. This behavior is called interning.
String a = "Hello";
String b = "World";
String c = "Hello";
System.out.println(a == c);//true because both variables reference the same String object
String a = "Hello";
String b = "World";
String c = "Hello";
System.out.println(a == c);//true because both variables reference the same String object
When reading Strings from the console or constructing them from byte[]s, a new String object is created even if one with the same content already exists.
String a = "Hello";
String b = IO.readln();
String c = new String(a.getBytes());
System.out.println(a == b);//false even if the user entered "Hello"
System.out.println(a == c);//false
String a = "Hello";
String b = IO.readln();
String c = new String(a.getBytes());
System.out.println(a == b);//false even if the user entered "Hello"
System.out.println(a == c);//false
📖 Sample answer from dan1st
MrMisha | Coder
MrMisha | Coder2mo ago
string interning is a process to save the string in string pool in java when you make a new string that will create a new object on heap but when you use a string interning that will create a copy in the string pool and the strings will point to this string and if you create a new var have the same string the jvm will search if that string is in the string pool and if it is exists will point to this string and if it is not exists will create a new string in the string pool and will make a point to it and that will save a memory in heap
public static void main(String[] args) {
String obj1 = new String("hi"); // -> that will create a new string object in the heap with value "hi"
String obj2 = new String("hi"); // -> and also this will do a new obj with value "hi"
// when comparing
System.out.println(obj1 == obj2)// that will return false because obj1 did not have a value that have the address of that value in the heap so obj1 have address ex: 3322b and that address point to the value in that address on heap and obj2 have address ex: 3345a and that point to the value place in the heap so the comparing will be System.out.println(3322b == 3345a) and that will be false
// using interning
Stirng str1 = "hello";
String str2 = "hello";
System.out.println(str1 == str2) // that will equal true because in str1 that create a string in string pool and the str1 will point to that address in the string pool ex: 2345b and str2 create a string in the string pooll and first before create a new one will search if that string is exist in the string pool and if it is exists will point to that string in the string pool address and so str2 will point to 2345b and in that case the System.out.println(2345b == 2345b) and that will be true
}
public static void main(String[] args) {
String obj1 = new String("hi"); // -> that will create a new string object in the heap with value "hi"
String obj2 = new String("hi"); // -> and also this will do a new obj with value "hi"
// when comparing
System.out.println(obj1 == obj2)// that will return false because obj1 did not have a value that have the address of that value in the heap so obj1 have address ex: 3322b and that address point to the value in that address on heap and obj2 have address ex: 3345a and that point to the value place in the heap so the comparing will be System.out.println(3322b == 3345a) and that will be false
// using interning
Stirng str1 = "hello";
String str2 = "hello";
System.out.println(str1 == str2) // that will equal true because in str1 that create a string in string pool and the str1 will point to that address in the string pool ex: 2345b and str2 create a string in the string pooll and first before create a new one will search if that string is exist in the string pool and if it is exists will point to that string in the string pool address and so str2 will point to 2345b and in that case the System.out.println(2345b == 2345b) and that will be true
}
Submission from arrm33

Did you find this page helpful?