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 this string, it returns the reference from the pool; if the pool does not contain this string, it adds it to the pool and returns the reference to this string.
The intern() method follows the following rules: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 poolalready containsthis string: return the reference of the existing string in the pool.
- If the string constant pooldoes not containthis string: store the reference of the current string in the heap into the constant pool (instead of creating a new object), and return that reference.
Java version differences:
In JDK 6 and earlier, the string constant pool is stored in the method area (PermGen), which has limited capacity; excessive use of intern() may lead to OutOfMemoryError.
In JDK 7 and above, the string constant pool has been moved to heap memory, allowing more data to be stored.
Example
The following example demonstrates the application of the intern() method:
Example
public static void main(String args[]) {
// str1 directly assigns the string literal "Example", which is stored in the string constant pool (String Pool)
String str1 = "Example";
// str2 is created using the new keyword, and it is stored in heap memory
String str2 = new String("Example");
// str3 is the reference obtained after str2 calls the intern() method
// The intern() method puts "Example" into the string constant pool and returns the reference in the pool
String str3 = str2.intern();
// Compare str1 and str2
// str1 points to "Example" in the constant pool, str2 points to the object in heap memory
// Since 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 "Example" in the constant pool
// str1 also points to "Example" in the constant pool, so their addresses are the same and the result is true
System.out.println(str1 == str3); // true
}
}
Code explanation:
String str1 = "Example";
-
Directly assigning a string literal
"Example", it willautomatically be stored in the string constant pool(String Pool).
String str2 = new String("Example");
-
Due to using the
newkeyword, it willcreate a new string object in the heap, that is,str2it does not point to the constant pool, but points to a new memory address.
String str3 = str2.intern();
-
intern()The method checks whether the string"Example"alreadyexists in the string constant pool:If it exists, return the reference of that string;
If it does not exist, add it to the constant pool and return the reference in the pool.
-
Since
str1already in the constant poolin,str3andstr1points to the same object.
System.out.println(str1 == str2);
-
str1points to the constant pool,str2points to the object in the heap, so==the comparison is of different memory addresses, the resultfalse。
System.out.println(str1 == str3);
-
str3Afterintern()that,points to the string in the constant pool, and withstr1points to the same object, resulttrue。
The output of the above program is:
false true
Other Extensions
Java String Class