AppDividend
Latest Code Tutorials

Java String intern() Method Example

0

The java.lang.String.intern() method is a native method of the Java String class. This means that this method is implemented in native coding using the Java Native Interface(JNI). The JNI specification governs the rules and guidelines for implementing native methods, this includes the rules for data type conversion, for example, to and from Java and the native application.

Java String intern()

String Interning is the method of storing only one copy of each distinct String Value, which must be immutable. By applying String.intern() on a couple of strings will ensure that all strings are having the same contents share the same memory.

See the following syntax.

Syntax

public native String intern()

Two different ways to create a String object in Java

To understand the working of the intern() method in Java, it is essential to understand the different ways in which a String object can be created in Java and how memory allocation takes place in the same.

Note: Recall that the == operator for String objects compares the memory locations of the two objects and not the values.

See the first example.

public class Example1 {
  public static void main(String[] args) {
    String str1 = new String("sample string");
    String str2 = "sample string";

    System.out.println(str1 == str2);
  }
}

Output

 

Java String intern() Method

In the above example, str1 is allocated memory in a heap, whereas str2 goes to the String Constant Pool; therefore, they have different memory locations.

See the second example.

public class Example2 {
  public static void main(String[] args) {
    String str1 = "sample string";
    String str2 = "sample string";

    System.out.println(str1 == str2);
  }
}

Output

 

Java String intern()

Here, both str1 and str2 are added to the String Constant Pool, and therefore, they have the same memory location.

Java String Constant Pool

Whenever a string object str1 is added in the String Constant Pool, which is maintained by the String class, the pool holds that string constant so that if another string str2 with the same value is created, additional memory is not allocated for str2 (unless requested specially by the new operator) and is given reference to the object already existing in the pool.

Java intern() method

Java intern() method in Java upon being called on a String object checks the String Constant Pool for String objects with the same value.

If such a String object is found, then reference to that object is returned. Otherwise, a String object with the given value is added to the String Constant Pool, and reference to this newly created String object is returned by the intern() method.

 

Java intern() method

Syntax : public native String intern()

Returns: Reference to String object with the same value from the String Constant Pool. If such a String object is not found in the pool, creates a new object in the pool and returns a reference to it.

The == operator vs. equals() method for String objects

Before moving on, here’s a quick recall to the difference between the use of the == operator and the equals() method for comparing String objects in Java.

See the following code example.

public class Example3 {
  public static void main(String[] args) {
    String str1 = new String("sample string");
    String str2 = "sample string";

    System.out.println(str1 == str2);
    System.out.println(str1.equals(str2));
  }
}

Output

 

The == operator vs. equals() method for String objects

As mentioned above, the == operator compares the memory locations of two strings, whereas the equals() method compares the values of the two strings.

Where they are used in this context is the following.

  1. == operator: To check whether a String object exists in the String Constant Pool or not.
  2. equals() method: To compare value existing in String Constant Pool with the value of the String object invoking the intern() method.

See the following working example.

public class Example4 {
  public static void main(String[] args) {
    String str1 = new String("sample string");
    String str2 = "sample string";
    String str3 = "sample string";

    System.out.println(str1 == str2);
    System.out.println(str2 == str3);

    str1 = str1.intern();

    System.out.println(str1 == str2);
  }
}

Output

 

equals() method

In the above example, in the statement str1=str1.intern(); the intern() method checks the String Constant Pool for a String object whose value matches that of String object str1.

Since there exists such a value, reference to it is returned, and therefore, when the memory locations are compared for the second time, str1==str2 returns true.  

Now, it was demonstrated in the first example, how the two strings are situated in different memory locations. Following up on that example, consider:

public class Example5 {
  public static void main(String[] args) {
    String str1 = new String("sample string");
    String str2 = str1.intern();
    String str3 = "sample string";

    System.out.println(str2 == str3);
  }
}

Output

 

Interning of String in Java

Here, str1 is allocated memory in a heap. So, when str1.intern() is invoked, there is no String object with value “sample string” in the String Constant Pool.

So, a String object with this value is created in the pool, and a reference to it is returned by the intern() method, which then gets stored in str2. Therefore, on comparing memory locations of str2 and str3, they are found to be the same as they both exist in the String Constant Pool.

This is because when str3 is created, a String object with the same value already exists in the String Constant Pool, and therefore, duplication is not done [Refer Section: Java String Constant Pool].

Finally, the Java String intern() Method Example is over.

Related Posts

Java String replace()

Java String replaceAll()

Java String format()

Java String GetBytes()

Java String toLowerCase()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.