Interning of String in Java
Last Updated :
02 Dec, 2024
String Interning in Java is a process of storing only one copy of each distinct String value, which must be immutable. Applying String.intern() on a couple of strings will ensure that all strings having the same contents that shares the same memory.
Example:
When a string is created using a string literal i.e. "hello",
Java checks if the string already exists in the String Pool. If it does, the existing reference is used; otherwise, the string is added to the pool.
Java
public class Main {
public static void main(String[] args) {
String s1 = "hello"; // s1 points to the String Pool
String s2 = "hello"; // s2 reuses the string from the pool
System.out.println(s1 == s2);
}
}
Explanation: Both s1
and s2
point to the same string object "hello"
in the String Pool, so s1 == s2
returns true
.
Diagrammatic Representation of String Interning in Java
In the diagram, the second reference str2
points to the String Constant Pool (SCP) where the string "Geeks"
already exists, demonstrating the concept of string interning in Java.
Important point: This can be very useful to reduce the memory requirements of your program. But be aware that the cache is maintained by JVM in a permanent memory pool which is usually limited in size compared to the heap so you should not use intern if you don't have too many duplicate values.
intern() Method
The intern()
method in Java is used to return the canonical representation of a string from the String Pool. When invoked, it checks whether the string exists in the pool:
- When the intern() method is executed, it checks whether the String equals to this String Object is in the pool.
- If it is available, 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.
- It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
It is advised to use equals(), not ==, to compare two strings. This is because the == operator compares memory locations, while the equals() method compares the content stored in two objects.
Examples of Interning of String
1. Using intern()
Method
JAVA
// Java program to illustrate intern() method
class GFG {
public static void main(String[] args) {
// S1 refers to object in the Heap Area
String s1 = new String("GFG");
// S2 refers to object in the SCP Area
String s2 = s1.intern();
// Comparing memory locations
System.out.println(s1 == s2);
// Comparing values
System.out.println(s1.equals(s2));
// S3 refers to object in the SCP Area
String s3 = "GFG";
// Comparing s2 and s3 in SCP
System.out.println(s2 == s3);
}
}
Explanation:
- When the string
"GFG"
is created using new String("GFG")
, it is stored in the Heap. - When
s1.intern()
is called, it checks the String Pool and places "GFG"
there, if not already present. s2
now refers to the same object in the SCP, while s1
still points to the object in the Heap.- Therefore,
s1 == s2
returns false
, but s1.equals(s2)
returns true
. s3
, created with the string literal "GFG"
, refers directly to the object in the String Pool, so s2 == s3
returns true
.
In the below diagram, s1 points to a string in the heap, while s2 and s3 point to the same string in the String Constant Pool, demonstrating the effect of string interning.
2. Using concat()
Method
JAVA
// Java program to illustrate intern() method
class GFG {
public static void main(String[] args) {
// S1 refers to object in the Heap Area
String s1 = new String("GFG");
// S2 refers to object in the Heap (after concat)
String s2 = s1.concat("GFG");
// S3 refers to object in SCP Area after intern()
String s3 = s2.intern();
System.out.println(s2 == s3);
// S4 refers to object in SCP Area
String s4 = "GFGGFG";
System.out.println(s3 == s4);
}
}
Explanation:
s1
creates a new string in the Heap.s2
is created by concatenating "GFG"
with "GFG"
, so it refers to a new object in the Heap.- When
s2.intern()
is called, it adds "GFGGFG"
to the String Pool, if not already present. s3
and s4
both refer to the same interned object in the String Pool, so s2 == s3
and s3 == s4
both return true
.
In the below diagram, the string "GFGGFG" is interned in the String Constant Pool, with s2
, s3
, and s4
all pointing to the same object, while s1
remains in the heap.
Key Points to Remember
- The String Constant Pool is where interned strings are stored to optimize memory.
- Using
intern()
can save memory but be cautious, as the String Pool is limited in size. - Interning ensures that identical strings share the same memory location by improving memory efficiency.