Java String intern() Method

Java String intern() Method | In this blog, we will discuss the string intern() method which is used to store only one copy of the different string values where it must be immutable. When we use an intern() method on some strings we can ensure that all the strings have a similar value and share the same memory.

For example, in code, we need to use the “Apple” string 99 times but by using the intern() method we can ensure that the memory allocated for the “Apple” string is only once. We use this method to reduce memory utilization. It can help us to improve the space of the memory.

When we use the intern() method it returns a canonical representation for the string object. A pool is usually maintained by string class. When this method is executed this check whether the string object is equal to the string in the pool or not, if equal then the string from the pool is returned, or else the string object is added to the pool and the reference to the string object will be returned.

While using the intern() method we usually advise you to use the equals() method to compare not the “==” method because the equals method compares the content of the string whereas the “==” method compares the memory location.

Java String Intern

public class Main {
   public static void main(String[] args) {
      String string1 = new String("Java");
      String string2 = string1.intern();

      System.out.println(string1 == string2);
      System.out.println(string1.equals(string2));

      String string3 = "Java";
      System.out.println(string1 == string3);
      System.out.println(string2 == string3);
      System.out.println(string1.equals(string3));
   }
}

Output:-

false
true
false
true
true

What is String Interning In Java?

When we use a new keyword, two string objects are created, one in the heap and the other in the pool, usually the reference is always done to the heap, now when the string1 is created two objects are been created and pointed to the heap area but when the string2 and string3 are created it points to the pool so when string2 and string3 are compared it returns true.

String string1 = new String("Java");
String string2 = string1.intern();
String string3 = "Java";

From the first line, the “Java” object is created in the string pool. In Java, every string literal is placed in the string pool. Another object is created for the variable “string” in the heap area and it points to the string pool “Java” object. Hence in the first line, 2 objects are created.

In the second line, one object is created for the variable “string2” and points to the string pool “Java” object. In the third line, JVM will check whether the given string literal “Java” is already present in the string pool or not. If it is available in the string pool then use it, else create an object for it and place it in the string pool so that it can be used multiple times without allocating new space. Since the “Java” string literal is already available in the string pool therefore its reference is assigned to the object created for the variable “string3”.

Java String Interning

public class Main {
   public static void main(String[] args) {
      String string1 = new String("Mango");
      String string2 = string1.concat("Mango");
      String string3 = string2.intern();
      System.out.println(string2 == string3);

      String string4 = "MangoMango";
      System.out.println(string3 == string4);
   }
}

Output:-

true
true

How String Works Internally In Java?

When the intern() method is used we get the reference for the object in the pool area in this case when the string2 is executed it creates only one object in the pool area then we try to intern string3 with the string2 which is again referenced to the pool area and the string4 is also created in the pool area hence the output is true.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *