Java String intern() Method Example

Java String.intern() method returns a canonical representation for the string object.

A pool of strings, initially empty, is maintained privately by the class String.

Java String intern() Method Example

public class InternExample {
    public static void main(String[] args) {
        String str = "javaguides";
        String newStr = new String("javaguides");
        System.out.println(newStr.intern().equals(str));
        System.out.println(newStr.equals(str));
        newStr.intern();
        str.intern();
    }
}

Output:
true true 

Reference

Comments