Easy Tutorial
❮ Java8 Method References Number Compareto ❯

Java intern() Method

Java String Class


The intern() method returns the canonical representation of the string object.

It follows these rules: For any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

Syntax

public String intern()

Parameters

-

None

Return Value

A string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.

Example

public class Test {
    public static void main(String args[]) {
        String Str1 = new String("www.tutorialpro.org");
        String Str2 = new String("WWW.tutorialpro.org");

        System.out.print("Canonical Representation: ");
        System.out.println(Str1.intern());

        System.out.print("Canonical Representation: ");
        System.out.println(Str2.intern());
    }
}

The output of the above program is:

Canonical Representation: www.tutorialpro.org
Canonical Representation: WWW.tutorialpro.org

Java String Class

❮ Java8 Method References Number Compareto ❯