GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java
  • Struts
  • Spring
  • Web
  • Miscellaneous
  • React
  • Database
  • Angular
  • Hire Me!
  • Write For Us

This view

  • Original page
  • New address

GeezerVison is showing websparrow.org — String intern() method in Java - Websparrow

String intern() method in Java

By Atul Rai | Last Updated: November 17, 2023 Previous       Next


In Java, the String class is one of the most fundamental classes, offering a wide array of methods to manipulate strings. One such method is intern(), which plays a unique role in managing string literals within the Java Virtual Machine (JVM).

What is the intern() method?

The intern() method is used to optimize memory by storing unique strings in the “string pool”. The string pool is a special area in the heap memory where Java keeps unique string literals. When intern() is invoked on a string, the JVM checks if an equivalent string is already present in the pool. If it finds a match, it returns a reference to that string. Otherwise, it adds the string to the pool and returns a reference to it.

How does intern() work?

Let’s consider an example to illustrate its usage:

String str1 = "Hello";
String str2 = new String("Hello").intern();

In this example:

  • str1 is a string literal, so it’s automatically added to the string pool.
  • str2 is explicitly interned using the intern() method, ensuring it references the same string object as str1.

Benefits of intern() method

  1. Memory Optimization: By using intern(), you can reduce memory consumption when dealing with multiple instances of the same string.
  2. Comparing Interned Strings: It becomes efficient to compare strings using == when both strings are interned, as it checks for reference equality.

Similar Post: Java 11- New Methods of String Class

Best Practices and Considerations

While the intern() method can be useful for certain scenarios, it’s essential to use it judiciously:

  1. Memory Overhead: The string pool can grow large if too many strings are interned, potentially impacting performance.
  2. Automatic Interning: String literals are automatically interned, so explicit use of intern() is typically necessary when dealing with dynamically created strings.

Let’s explore scenarios where intern() proves beneficial:

Scenario 1: Using intern() for String Comparison

String str1 = "apple";
String str2 = new String("apple").intern();

System.out.println(str1 == str2); // Outputs: true

Here, both str1 and str2 reference the same interned string, resulting in true for the comparison.

Scenario 2: String Pool Management

String bigString = ""; // Initialize an empty string
for (int i = 0; i < 100000; i++) {
    bigString += i;
}
bigString = bigString.intern();

In this scenario, bigString is constructed by appending numbers repeatedly. Interning bigString helps prevent the accumulation of too many unique string instances in memory.

Summary

The intern() method in Java’s String class is a powerful tool for managing string literals and optimizing memory usage. However, it should be used judiciously, considering its impact on memory and performance. By intelligently using intern(), developers can efficiently manage string literals and enhance their application’s performance.

References

  1. String’s intern – JavaDoc
  2. Java 11- New Methods of String Class
intern Java method string

Previous

      Next

Similar Posts

  • Java Stream map() vs flatMap() Method
  • Java Collections.min() and Collections.max() Methods
  • Capitalize the first letter of each word in a String using Java
  • Java 17 Sealed Classes
  • Java 8 – How to sort Set with stream.sorted()
  • Find the closest pair from Array to a target value in Java
  • Java Pattern Matching for Switch
  • How to create Token in Java
  • Java 8- Find the nth Highest Salary
  • Java 9- Creating Collection using Factory Method of(…)

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.