Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaInterning of String in Java

Interning of String in Java

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

For example:

if the name ‘Amy’ appears 100 times, by interning you ensure only one ‘Amy’ is actually allocated memory. 
 

String Interning in Java

 

This can be very useful to reduce the memory requirements of your program. But be aware that the cache is maintained by JVM in a permanent memory pool which is usually limited in size compared to the heap so you should not use intern if you don’t have too many duplicate values

intern() Method

In Java, when we perform any operation using the intern() method, it returns a canonical representation for the string object. A pool is managed by a String class.

  • When the intern() method is executed, it checks whether the String equals to this String Object is in the pool.
  • If it is available, 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.
  • It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

It is advised to use equals(), not ==, to compare two strings. This is because the == operator compares memory locations, while the equals() method compares the content stored in two objects. 

Examples of Interning of String

Example 1:

JAVA




// Java program to illustrate
// intern() method
class GFG {
    public static void main(String[] args)
    {
        // S1 refers to Object in the Heap Area
        // Line-1
        String s1 = new String("GFG");
 
        // S2 refers to Object in SCP Area
        // Line-2
        String s2 = s1.intern();
 
        // Comparing memory locations
        // s1 is in Heap
        // s2 is in SCP
        System.out.println(s1 == s2);
 
        // Comparing only values
        System.out.println(s1.equals(s2));
 
        // S3 refers to Object in the SCP Area
        // Line-3
        String s3 = "GFG ";
 
        System.out.println(s2 == s3);
    }
}


Output

false
true
false

Explanation of the above method

Whenever we create a String Object, two objects will be created i.e. One in the Heap Area and One in the String constant pool and the String object reference always points to the heap area object. When line-1 executes, it will create two objects and point to the heap area created object. Now when line-2 executes, it will refer to the object which is in the SCP. Again when line-3 executes, it refers to the same object which is in the SCP area because the content is already available in the SCP area. No need to create a new object. 

Java string interning

 

 

If the corresponding String constant pool(SCP) object is not available then the intern() method itself will create the corresponding SCP object. 

Example 2:

JAVA




// Java program to illustrate
// intern() method
class GFG {
    public static void main(String[] args)
    {
        // S1 refers to Object in the Heap Area
        // Line-1
        String s1 = new String("GFG");
 
        // S2 now refers to Object in SCP Area
        // Line-2
        String s2 = s1.concat("GFG");
 
        // S3 refers to Object in SCP Area
        // Line-3
        String s3 = s2.intern();
 
        System.out.println(s2 == s3);
 
        // S4 refers to Object in the SCP Area
        // Line-4
        String s4 = "GFGGFG";
 
        System.out.println(s3 == s4);
    }
}


Output

true
true

Explanation of the above method

We use the intern() method to get the reference of the corresponding SCP Object. In this case, when Line-2 executes s2 will have the value “GFGGFG” in it only creates one object. In Line-3, we try to intern s3 which is again with s2 in the SCP area. s4 is also in SCP so all give output as true when compared. 

interning of string in Java

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments