Java String Intern
# Java intern() Method
[Java String Class](#)
* * *
The `intern()` method is used to add a string to the internal string pool at runtime and returns the reference from the string pool.
When the `intern()` method is called, if the string constant pool already contains that string, it returns the reference from the pool. If the pool does not contain the string, it adds the string to the pool and returns the reference to that string.
**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.
```java
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
```java
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 reference of the current string from the heap into the constant pool (instead of 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 (PermGen), which had limited capacity. Excessive use of `intern()` could lead to `OutOfMemoryError`.
* In JDK 7 and above, the string constant pool was moved to the heap memory, allowing it to store more data.
### Example
The following example demonstrates the application of the `intern()` method:
## Example
```java
public class TutorialTest {
public static void main(String args[]){
// str1 directly assigns the string literal "", which is stored in the string constant pool (String Pool)
String str1 ="";
// str2 is created using the new keyword, which is stored in the heap memory
String str2 =new String("");
// str3 is the reference obtained after calling the intern() method on str2
// The intern() method places "" into the string constant pool and returns the reference from the pool
String str3 = str2.intern();
// Comparing str1 and str2
// str1 points to "" in the constant pool, str2 points to an object in the heap memory
// Since their addresses are different, the result is false
System.out.println(str1 == str2);// false
// Comparing str1 and str3
// str3 is the reference returned after calling the intern() method on str2, pointing to "" in the constant pool
// str1 also points to "" in the constant pool, so their addresses are the same, the result is true
System.out.println(str1 == str3);// true
}
}
**Code Explanation:**
`String str1 = "";`
* **Directly assigns the string literal** `""`, which is **automatically stored in the string constant pool** (String Pool).
`String str2 = new String("");`
* Because the `new` keyword is used, it **creates a new string object in the heap (Heap)**. That is, `str2` **does not point to the constant pool**, but to a new memory address.
`String str3 = str2.intern();`
* The `intern()` method checks if the string `""` **already exists in the string constant pool**:
* **If it exists**, it returns the reference to that string;
* **If it does not exist**, it adds it to the constant pool and returns the reference from the pool.
* Since `str1` **is already in the constant pool**, `str3` and `str1` point to the same object.
`System.out.println(str1 == str2);`
* `str1` **points to the constant pool**, `str2` **points to an object in the heap**, so `==` compares different memory addresses, resulting in `false`.
`System.out.println(str1 == str3);`
* After `intern()`, `str3` **points to the string in the constant pool**, pointing to the same object as `str1`, resulting in `true`.
The output of the above program is:
false
true
* * Java String Class](#)
YouTip