Java String intern()

The intern() method in Java is used to return a canonical representation of the String object. In other words, it returns a reference to the string object from the pool of strings, which can be shared across multiple classes or threads.

The String class in Java is immutable, meaning that once a String object is created, it cannot be modified. When a String object is created, it is stored in the heap memory. However, the intern() method allows the String object to be stored in the pool of strings, which is stored in a special memory area called the string constant pool. The string constant pool is a part of the Java runtime environment, and it contains a collection of unique String objects that are referenced by the JVM.

When the intern() method is called on a String object, it first checks if a string with the same value is already present in the pool of strings. If a string with the same value is present, then the intern() method returns the reference to that string object. If not, then a new string object is created in the pool of strings, and the intern() method returns the reference to that object.

The main advantage of using the intern() method is that it can help reduce memory usage in Java programs, especially when working with large amounts of string data. By storing String objects in the pool of strings, the JVM can reuse these objects across different parts of the program, reducing the number of String objects that need to be created and stored in memory.

Signature of Java String intern():

The signature of the intern() method in Java is as follows:

public native String intern();

This method is a member of the String class and is declared as a public instance method. The native keyword indicates that the implementation of the intern() method is written in native code, meaning that it is implemented in a language other than Java, typically in the C or C++ programming languages.

The return type of the intern() method is String, which is the same as the type of the String object on which the method is called. The intern() method does not take any parameters. When called on a String object, it returns a canonical representation of the string, which is stored in the pool of strings.

Returns of Java String intern():

The intern() method in Java returns a String object that is a canonical representation of the original String object. The returned String object may or may not be the same as the original String object, depending on whether an equal string is already present in the pool of strings.

If a string with the same value as the original string is already present in the pool of strings, then the intern() method returns a reference to that string object. Otherwise, a new String object is created in the pool of strings, and the intern() method returns a reference to that object.

It is important to note that the intern() method does not modify the original String object. Instead, it returns a new String object that is a canonical representation of the original object. Therefore, it is necessary to assign the result of the intern() method to a new String object in order to use the canonical representation of the string.

Here is an example of how to use the intern() method:

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

// str2 is a reference to the canonical representation of the "Hello World" string in the pool of strings
// str1 and str2 are equal but not necessarily the same object

The need and working of the String.intern() Method:

The intern() method in Java is used to return a canonical representation of the String object. It can be useful in situations where a large number of String objects are created with the same value, as it allows the JVM to reuse String objects and reduce memory usage.

When a String object is created in Java, it is stored in the heap memory. The intern() method allows the String object to be stored in the pool of strings, which is stored in a special memory area called the string constant pool. The string constant pool is a part of the Java runtime environment, and it contains a collection of unique String objects that are referenced by the JVM.

The intern() method works by first checking if the string value of the String object is already present in the pool of strings. If it is, then the intern() method returns a reference to that string object. If not, then a new String object is created in the pool of strings, and the intern() method returns a reference to that object.

Here is an example that demonstrates how the intern() method works:

String str1 = new String("Hello World");  // creates a new String object in the heap memory
String str2 = str1.intern();              // checks if the string value of str1 is already in the pool of strings

// str1 and str2 are equal but not necessarily the same object

In this example, the String object str1 is created in the heap memory using the new keyword. When the intern() method is called on str1, it checks if the string value of str1 (“Hello World”) is already present in the pool of strings. Since the string value “Hello World” is a commonly used string in Java, it is likely that it is already present in the pool of strings. Therefore, the intern() method returns a reference to the existing String object in the pool of strings.

By using the intern() method, we can reduce memory usage in our Java program by ensuring that only one copy of each unique String value is stored in the pool of strings.

Java String intern() Method Example:

Here is an example of how to use the intern() method in Java:

public class StringInternExample {
    public static void main(String[] args) {
        String str1 = new String("Hello World");   // creates a new String object in the heap memory
        String str2 = "Hello World";               // stores the string in the pool of strings

        // compare the two String objects using the == operator
        System.out.println("str1 == str2: " + (str1 == str2));   // prints false

        // intern the str1 String object to return a canonical representation
        str1 = str1.intern();

        // compare the two String objects using the == operator
        System.out.println("str1 == str2: " + (str1 == str2));   // prints true
    }
}

In this example, we first create a new String object str1 using the new keyword. This creates a new String object in the heap memory.

We also create a String object str2 by simply assigning it the string value “Hello World”. Since this value is a commonly used string in Java, it is likely that it is already present in the pool of strings. Therefore, the String object str2 is stored in the pool of strings.

We then compare the two String objects str1 and str2 using the == operator. Since they are two separate objects, the comparison returns false.

We then call the intern() method on the str1 object to return a canonical representation of the string. This method checks if the string value of str1 is already present in the pool of strings. Since the string value “Hello World” is already present in the pool of strings, the intern() method returns a reference to the existing String object.

We then compare the two String objects str1 and str2 using the == operator again. Since they now reference the same object in the pool of strings, the comparison returns true.

Points to Remember:

Here are some key points to remember about the intern() method in Java:

  • The intern() method returns a canonical representation of a String object.
  • The canonical representation of a String object is stored in the string constant pool, which is a special memory area of the Java runtime environment.
  • If a String object is already present in the pool of strings, then the intern() method returns a reference to that object. Otherwise, it creates a new String object in the pool of strings and returns a reference to that object.
  • The intern() method can be used to reduce memory usage in Java programs by ensuring that only one copy of each unique String value is stored in the pool of strings.
  • The intern() method can be called on any String object, including string literals and String objects created using the new keyword.
  • The intern() method can be useful when working with large numbers of String objects that have the same value, as it allows the JVM to reuse String objects and reduce memory usage. However, it should be used with caution, as it can also increase the time and overhead required to manage the string constant pool.