A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/interning-of-string/ below:

Interning of String in Java

Interning of String in Java

Last Updated : 02 Dec, 2024

String Interning in Java is a process 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 that shares the same memory.

Example:

When a string is created using a string literal i.e. "hello", Java checks if the string already exists in the String Pool. If it does, the existing reference is used; otherwise, the string is added to the pool.

Java
public class Main {
  
    public static void main(String[] args) {
      
        String s1 = "hello";     // s1 points to the String Pool
        String s2 = "hello";     // s2 reuses the string from the pool

        System.out.println(s1 == s2);   
    }
}

Explanation: Both s1 and s2 point to the same string object "hello" in the String Pool, so s1 == s2 returns true.

Diagrammatic Representation of String Interning in Java

In the diagram, the second reference str2 points to the String Constant Pool (SCP) where the string "Geeks" already exists, demonstrating the concept of string interning in Java. 

Important point: 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

The intern() method in Java is used to return the canonical representation of a string from the String Pool. When invoked, it checks whether the string exists in the pool:

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 1. Using intern() Method JAVA
// Java program to illustrate intern() method
class GFG {
  
    public static void main(String[] args) {
      
        // S1 refers to object in the Heap Area
        String s1 = new String("GFG");
        
        // S2 refers to object in the SCP Area
        String s2 = s1.intern();
        
        // Comparing memory locations
        System.out.println(s1 == s2);  
        
        // Comparing values
        System.out.println(s1.equals(s2));  
        
        // S3 refers to object in the SCP Area
        String s3 = "GFG";
        
        // Comparing s2 and s3 in SCP
        System.out.println(s2 == s3);  
    }
}

Explanation:

In the below diagram, s1 points to a string in the heap, while s2 and s3 point to the same string in the String Constant Pool, demonstrating the effect of string interning.

2. Using concat() Method JAVA
// Java program to illustrate intern() method
class GFG {
  
    public static void main(String[] args) {
      
        // S1 refers to object in the Heap Area
        String s1 = new String("GFG");
        
        // S2 refers to object in the Heap (after concat)
        String s2 = s1.concat("GFG");
        
        // S3 refers to object in SCP Area after intern()
        String s3 = s2.intern();
        
        System.out.println(s2 == s3);  
        
        // S4 refers to object in SCP Area
        String s4 = "GFGGFG";
        
        System.out.println(s3 == s4);  
    }
}

Explanation:

In the below diagram, the string "GFGGFG" is interned in the String Constant Pool, with s2, s3, and s4 all pointing to the same object, while s1 remains in the heap.

Key Points to Remember

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4