Tuesday, August 16, 2022

intern() Method in Java String

In this post we’ll discuss Java String intern() method in detail.

String interning process

In Java String literals are interned that means only one copy of distinct String values is stored in the constant string pool. If there are more than one string literals having the same value those string instances share the same reference in the pool. Thus string interning helps in reducing memory as new space is not required for a String object having the same value as any existing instance.

If String with same value is not found in the pool the new value is added to the pool and its reference is returned.

For example, if two Strings are created as follows-

String str1 = “abc”;
String str2 = “abc”;

Then the reference is shared by both the objects.

Java string interning

You can also check it using a Java program by creating two String literal having same value and then compare their references using equality ‘==’ operator, if true is returned that means both have the same reference.

public class StringDemo {

 public static void main(String[] args) {
  String str1 = "abc";
  String str2 = "abc";
  if(str1 == str2){
   System.out.println("str1 and str2 are same");
  }else{
   System.out.println("str1 and str2 are not same");
  }
 }
}

Output

str1 and str2 are same

Java String intern() method

By using intern() method in Java you can explicitly intern a string i.e. when the intern method is invoked, if the pool already contains a string equal to this String object, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

Now you may ask if Strings are automatically interned then what is the use of intern method in the Java String class and what is the need to explicitly intern a string?

For that you have to know how String is created when new operator is used to create a String in Java. When you create a String object using new operator, it always creates a new object in heap memory.

For example, if three Strings are created as follows-

String str1 = “abc”;
String str2 = new String(“abc”);
String str3 = new String(“abc”);

Then str1 is stored in a pool where as new objects are created for str2 and str3.

string creation new operator

Here is a Java example to check the same. If we create two more strings using new operator and then compare reference those references should be different.

public class StringDemo {
  public static void main(String[] args) {
    // Literals
    String str1 = "abc";
    String str2 = "abc";
    if(str1 == str2){
      System.out.println("str1 and str2 are same");
    }else{
      System.out.println("str1 and str2 are not same");
    }
    // Using new operator
    String str3 = new String("abc");
    String str4 = new String("abc");
    if(str3 == str4){
      System.out.println("str3 and str4 are same");
    }else{
      System.out.println("str3 and str4 are not same");
    }

    if(str1 == str4){
      System.out.println("str1 and str4 are same");
    }else{
      System.out.println("str1 and str4 are not same");
    }
  }
}

Output

str1 and str2 are same
str3 and str4 are not same
str1 and str4 are not same

So, bottom line is only String literals are interned. For taking advantage of interning and string pool you have to use intern() method on Strings that are created using new operator.

Intern() method Java example

In the example one of the String which is created using new operator is interned too using intern() method.

public class StringDemo {

 public static void main(String[] args) {
  String str1 = "abc";
  String str2 = "abc";
  if(str1 == str2){
   System.out.println("str1 and str2 are same");
  }else{
   System.out.println("str1 and str2 are not same");
  }
  String str3 = new String("abc");
  String str4 = new String("abc").intern();
  if(str3 == str4){
   System.out.println("str3 and str4 are same");
  }else{
   System.out.println("str3 and str4 are not same");
  }
  
  if(str1 == str4){
   System.out.println("str1 and str4 are same");
  }else{
   System.out.println("str1 and str4 are not same");
  }
 }
}

Output

str1 and str2 are same
str3 and str4 are not same
str1 and str4 are same

It can be seen that str1 (String literal so already uses String pool) and str4 (Explicitly interned using intern() method) are having the same reference now.

That's all for this topic intern() Method in Java String. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related topics

  1. Java trim(), strip() - Removing Spaces From String
  2. Check String Null or Empty in Java
  3. String Comparison in Java equals(), compareTo(), startsWith() Methods
  4. Converting String to Char Array in Java
  5. Removing Spaces Between Words in a String Java Program

You may also like-

  1. Java do-while Loop With Examples
  2. Switch-Case Statement in Java
  3. ArrayList in Java With Examples
  4. Synchronization in Java - Synchronized Method And Block
  5. New Date And Time API in Java 8
  6. How to Inject Null And Empty String Values in Spring
  7. Spring Integration With Quartz Scheduler
  8. Python First Program - Hello World

No comments:

Post a Comment