Java intern() method

    intern() method

    This method will provide you with the canonical representation of the string. The string will be returned from memory if it is created using the new keyword. It will also create a copy of the object of the string from the heap in the constant pool.

    Syntax-

    public String intern()  

    Example-

    public class Simple{  
        public static void main(String[] args) {      
    
           String s1 = new String("welcome");
           String s2="hello";
           String s3=s1.intern();
           System.out.println(s1==s2);
           System.out.println(s2==s3);
    
         }
    }  

    Output-

    Java Intern Method