Java String intern()

Filed Under: Java

Java String intern() is a native method. When the intern() method is invoked on a String object, if the String Pool already has a String with the same value, then the reference of String from the Pool is returned. Otherwise, this string object is added to the pool and the reference is returned.

Java String Intern

Java String Intern

Java String intern()

Let’s try to understand intern() method with a simple program.


package com.journaldev.string;

public class StringIntern {

	public static void main(String args[]) {

		String s1 = new String("abc"); // goes to Heap Memory, like other objects
		String s2 = "abc"; // goes to String Pool
		String s3 = "abc"; // again, goes to String Pool

		// Let's check out above theories by checking references
		System.out.println("s1==s2? " + (s1 == s2)); // should be false
		System.out.println("s2==s3? " + (s2 == s3)); // should be true

		// Let's call intern() method on s1 now
		s1 = s1.intern(); // this should return the String with same value, BUT from String Pool

		// Let's run the test again
		System.out.println("s1==s2? " + (s1 == s2)); // should be true now

	}
}

Output:


s1==s2? false
s2==s3? true
s1==s2? true

String intern() Example Explanation

  1. When we are using new operator, the String is created in the heap space. So “s1” object is created in the heap memory with value “abc”.
  2. When we create string literal, it’s created in the string pool. So “s2” and “s3” are referring to string object in the pool having value “abc”.
  3. In the first print statement, s1 and s2 are referring to different objects. Hence s1==s2 is returning false.
  4. In the second print statement, s2 and s3 are referring to the same object in the pool. Hence s2==s3 is returning true.
  5. Now, when we are calling s1.intern(), JVM checks if there is any string in the pool with value “abc” is present? Since there is a string object in the pool with value “abc”, its reference is returned.
  6. Notice that we are calling s1 = s1.intern(), so the s1 is now referring to the string pool object having value “abc”.
  7. At this point, all the three string objects are referring to the same object in the string pool. Hence s1==s2 is returning true now.

Please watch the below YouTube video for better clarity about the intern() method.

You can checkout more String examples from our GitHub Repository.

Reference: API Doc

Comments

  1. Rohit pawar says:

    String s1 = new String(“GFG”);

    // S2 now refers to Object in SCP Area or heap because its the case of runtime

    String s2 = s1.concat(“GFG”);
    //s2 points an obj in heap and SCP not containing its copy

    String s3 = s2.intern(); //this line basically search s2 like object in SCP and it can’t find then it create s2 like object in SCP right

    // so s2 points heap object and s3 points SCP object am i right or wrong

    System.out.println(s2 == s3); //if am i right then why this Statement return true ,it has to print false because s2 and s3 points different objects.

    // S4 refers to Object in the SCP Area
    String s4 = “GFGGFG”; // Line-4

    System.out.println(s3 == s4);

    1. Venkatesh.E says:

      The string inside s1 will be appended to “GFG” and form “GFGGFG” and this is a String constant.
      String.contact() create a string as output and not new String object and hence it is stored inside String Pool area.

      So if you use “s2.intern()” (doesn’t make sense using it), it still returns the same memory location from String Pool and so “s2 == s3” return true.

      with ‘s4’ you are directly assigning a constant and this string already exists inside a String Pool and so instead of adding a new string to the pool, it will return the memory location of already existing one (“GFGGFG”).

      So s2, s3, s4 all three referencing the same string object inside pool and so using “==” returns true when compared.

  2. SK Sharma says:

    This is best explanation among all the article over web.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages