Java intern() Method
The intern() method is used to add a string to the internal string pool at runtime and return the reference from the string pool.
When the intern() method is called, if the string constant pool already contains the string, the reference in the pool is returned; if the pool does not contain the string, it is added to the pool and the string's reference is returned.
**The intern() method follows this rule:** for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.
String s1 = new String("Hello");
String s2 = "Hello";
String s3 = s1.intern();
System.out.println(s1 == s2); // false
System.out.println(s2 == s3); // true
Syntax
public String intern()
Parameters
-
None
Return Value
The return value of the intern() method is the reference of the string in the string constant pool (String Pool).
Return value rules:
- If the string constant pool already contains the string: returns the reference of the existing string in the pool.
- If the string constant pool does not contain the string: stores the string reference from the current heap into the constant pool (rather than creating a new object), and returns that reference.
Java Version Differences:
-
In JDK 6 and earlier, the string constant pool was stored in the method area (permanent generation), with limited capacity; excessive use of intern() could cause OutOfMemoryError.
-
In JDK 7 and later, the string constant pool was moved to heap memory, allowing more data to be stored.
Example
The following example demonstrates the use of the intern() method:
Example
public
class
RunoobTest
{
public
static
void
main
(
String
args
[
]
)
{
// str1 is directly assigned the string literal "Runoob", which is stored in the string constant pool (String Pool)
String
str1
=
"Runoob"
;
// str2 is created with the new keyword and is stored in heap memory
String
str2
=
new
String
(
"Runoob"
)
;
// str3 is the reference obtained after calling the intern() method on str2
// The intern() method puts "Runoob" into the string constant pool and returns the reference in the pool
String
str3
=
str2.
intern
(
)
;
// Compare str1 and str2
// str1 points to "Runoob" in the constant pool, while str2 points to an object in heap memory
// Because their addresses are different, the result is false
System
.
out
.
println
(
str1
==
str2
)
;
// false
// Compare str1 and str3
// str3 is the reference returned after str2 calls the intern() method, and it points to "Runoob" in the constant pool
// Since str1 also points to "Runoob" in the constant pool, their addresses are the same and the result is true
System
.
out
.
println
(
str1
==
str3
)
;
// true
}
}
Code explanation:
String str1 = "Runoob";
-
Directly assigning a string literal
"Runoob", which willautomatically be stored in the string constant pool(String Pool).
String str2 = new String("Runoob");
-
Because it uses the
newkeyword, it willcreate a new string object in the heap, which means itstr2does not point to the constant pool, but to a new memory address.
String str3 = str2.intern();
-
intern()The method checks whether the string"Runoob"has alreadyexisted in the string constant pool: -
If it exists, returns the reference of that string;
-
If it does not exist, adds it to the constant pool and returns the reference in the pool.
If it exists, returns the reference of that string;
-If it does not exist, adds it to the constant pool and returns the reference in the pool.
-
Because
str1is already in the constant poolin,str3andstr1point to the same object.
System.out.println(str1 == str2);
-
str1points to the constant pool,str2points to an object in the heap, so==compares different memory addresses, and the resultfalse.
System.out.println(str1 == str3);
-
str3Afterintern(),points to the string in the constant pool, and withstr1points to the same object, and the resulttrue.
The execution result of the above program is:
false
true
Java String class