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.
Similar Reads
String to int in Java
Converting a String to an int in Java can be done using methods provided in the Integer class, such as Integer.parseInt() or Integer.valueOf() methods. Example: The most common method to convert a string to a primitive int is Integer.parseInt(). It throws a NumberFormatException if the string contai
2 min read
StringBuffer insert() in Java
The StringBuffer.insert() method in Java allows us to insert a string representation of a given data type at a specified position in a StringBuffer. This method is useful when we need to modify a string at specific positions without creating a new string each time by making it more efficient than co
4 min read
String Constant Pool in Java
In Java, Strings are the type of objects that can store the character of values. A string acts the same as an array of characters in Java. Memory allocation is not possible without String Constant Pool. In this article, we will learn about Java String Constant Pool. What is Java String Pool?A Java S
5 min read
Java String indexOf()
In Java, the String indexOf() method returns the position of the first occurrence of the specified character or string in a specified string. In this article, we will learn various ways to use indexOf() in Java. Example: Below is the simplest way that returns the index of the first occurrence of a s
3 min read
Storage of String in Java
In Java, we know both Stack and Heap space are part of Java Virtual Machine (JVM). However, these memory spaces are used for different purposes. Stack space contains specific values that are short-lived whereas Heap space is by Java Runtime to allocate memory to objects and JRE classes. In Java, str
3 min read
Swapping Characters of a String in Java
As we know that Object of String in Java are immutable (i.e. we cannot perform any changes once its created). To do modifications on string stored in a String object, we copy it to a character array, StringBuffer, etc and do modifications on the copy object.In this article we would go through some m
3 min read
Printing Integer between Strings in Java
Try to figure out the output of this code: public class Test { public static void main(String[] args) { System.out.println(45+5 + "=" +45+5); } } Output: 50=455 The reason behind this is - Initially the integers are added and we get the L.H.S. as 50. But, as soon as a string is encountere
1 min read
String Arrays in Java
A String Array in Java is an array that stores string values. The string is nothing but an object representing a sequence of char values. Strings are immutable in Java, this means their values cannot be modified once created. When we create an array of type String in Java, it is called a String Arra
5 min read
Applications of String indexOf() Method in Java
There are four variants of indexOf() method. indexOf(): This method returns the index within this string of the first occurrence of the specified character or -1 if the character does not occur.indexOf(char ch, int start ): This method returns the index within this string of the first occurrence of
3 min read
Java Convert int to String - Different Ways of Conversion
Converting an int to a String is an important type conversion. Many operations can be performed over a string, while we are limited when it comes to integers. We have a wide varied list of in-built methods in the String class that help us perform hassle-free operations. Suppose we are required to co
9 min read