DEV Community

Cover image for What Happens Internally When You Call String intern in Java
Gowtham Kalyan
Gowtham Kalyan

Posted on

What Happens Internally When You Call String intern in Java

1. Introduction

The intern method in Java is used to manage memory efficiently by storing string objects in a special memory area called the String Pool. Understanding how String.intern() works internally is important for writing optimized Java applications.

2. What is String Pool

The String Pool is a special memory area in the heap where unique string literals are stored. It helps in saving memory by reusing existing string objects instead of creating new ones.

Summary

Stores unique string values.
Avoids duplicate objects.
Improves memory efficiency.

3. What is String intern Method

The intern() method checks whether a string already exists in the String Pool. If it exists, it returns the reference of that string. If not, it adds the string to the pool and returns its reference.

Syntax

java
String s = str.intern();
Enter fullscreen mode Exit fullscreen mode

Summary

Returns pooled string reference.
Adds string to pool if not present.

4. Internal Working of String intern

When you call intern() on a string

The JVM checks the String Pool.
If the string already exists, it returns the existing reference.
If the string does not exist, it is added to the pool.
A reference to the pooled string is returned.

Example


java id="q4k8xp"
public class InternExample {
    public static void main(String[] args) {
        String s1 = new String("Java");
        String s2 = s1.intern();

        String s3 = "Java";

        System.out.println(s2 == s3); // true
    }
}

Enter fullscreen mode Exit fullscreen mode

Explanation

s1 creates a new object in heap.
intern adds or retrieves the string from pool.
s3 directly refers to pooled string.
Both s2 and s3 point to same reference.

5. Behavior Before and After Java 7

Before Java 7
String Pool was stored in PermGen memory.

After Java 7
String Pool is moved to heap memory, making it more flexible and scalable.

Summary

Improved memory management.
Better scalability.

6. Advantages of Using intern Method

Reduces memory usage.
Avoids duplicate string objects.
Improves performance in certain cases.

7. When to Use intern Method

Use it when there are many duplicate strings.
Useful in memory-sensitive applications.
Avoid overusing it unnecessarily.

8. Common Mistakes to Avoid

Assuming intern always improves performance.
Using it excessively can increase overhead.
Not understanding heap and pool behavior.

9. Key Takeaways

intern method works with String Pool.
Ensures only one copy of string exists.
Returns reference from pool.
Helps optimize memory usage.

10. Useful Resources

Learn more from the AI powered Core JAVA Online Training in Hyderabad.
https://www.ashokit.in/courses/core-java-online-training

Follow the Java Full Stack Developer Roadmap to become job ready.
https://www.ashokit.in/java-full-stack-developer-roadmap

11. FAQ Section

11.1 What does String intern do in Java

It returns a reference from the String Pool if the string exists, otherwise it adds the string to the pool and returns the reference.

11.2 Where is the String Pool stored

After Java 7, the String Pool is stored in heap memory.

11.3 Does intern create a new object

It creates a new object in the pool only if it does not already exist.

11.4 Why use intern method

It helps reduce memory usage by avoiding duplicate string objects.

11.5 Is intern method always recommended

No, it should be used only when necessary because it may add overhead.

12. Conclusion

The String.intern() method plays a key role in memory optimization by managing string objects in the String Pool. By understanding its internal working, you can write more efficient Java programs. To gain hands-on experience, consider learning from the AI powered Core JAVA Online Training in Hyderabad.

13. Promotional Content

Start learning today with the AI powered Core JAVA Online Training in Hyderabad.

Top comments (0)