Skip to content

Latest commit

 

History

History
70 lines (54 loc) · 3.16 KB

18. String Constant Pool (SCP).md

File metadata and controls

70 lines (54 loc) · 3.16 KB

18. String Constant Pool (SCP)

String str1 = "hello";
String str2 = new String("hello");
String str3 = new String("hello");
String str4 = "hello";

System.out.println(str1 == str2); // false
System.out.println(str2 == str3); // false
System.out.println(str3 == "hello"); // false
System.out.println(str4 == "hello"); // true
System.out.println(str1 == str4); // true 

== operator checks the memory location by default.

when a string is created using a constructor signature, a new instance of string is created and a memory is allocated in a heap.

when a string is created using a string literal, java checks if the another String with same content exists in the string pool , if exists new string object will not be created and the new reference will point to the existing string in the pool. but if does not exists a new string object is created in heap and added to the string pool for reuse.

String str1 = "hello"; // creates a string and add its to the string pool
String str2 = new String("hello"); // new string instance created in heap
String str3 = new String("hello"); // new string instance created in heap
String str4 = "hello"; // already exists in string pool so points to that in pool

// == operator checks the memory location instead of value

System.out.println(str1 == str2); // false
System.out.println(str2 == str3); // false
System.out.println(str3 == "hello"); // false
System.out.println(str4 == "hello"); // true, "hello" is using string literal
System.out.println(str1 == str4); // true 

// How to compare or check if two string are equal by value
System.out.println(str1.equals(str2)); // true
System.out.println(str2.equals(str3)); // true
System.out.println(str3.equals("hello")); // true
System.out.println(str4.equals("hello")); // true
System.out.println(str1.equals(str4)); // true 

// text block
String strName = "John Doe";
String textBlock = """
        John Doe""";
System.out.println(strName.equals(textBlock)); // true
System.out.println(strName == textBlock); // true
System.out.println(strName);
System.out.println(textBlock);

We know that creating an object is a costly operation in Java. Therefore, to save time, Java developers came up with the concept of String Constant Pool (SCP). The SCP is an area inside the heap memory. It contains the unique strings. In order to put the strings in the string pool, one needs to call the intern() method. Before creating an object in the string pool, the JVM checks whether the string is already present in the pool or not. If the string is present, its reference is returned.

String str = new String("Welcome to JavaTpoint").intern(); 
String str1 = new String("Welcome to JavaTpoint").intern(); 
System.out.println(str1 == str); // true  

String str2 = new String("hello world");
String str3 = str2.intern();
String str4 = "hello world";

System.out.println(str2 == str3); // false
System.out.println(str3 == str4); // true
System.out.println(str2 == str4); // false

String s = "d".intern(); and String p = "d"; is treated the same by the java compiler. string literal always invoke the intern() method whether one mentions the intern method along with the string literal or not.