Java Data Type Tutorial - Java String.intern()








Syntax

String.intern() has the following syntax.

public String intern()

Example

In the following code shows how to use String.intern() method.

public class Main {
//from   w  w w. ja va 2s.  c  o  m
  public static void main(String[] args) {
    String str1 = "java2s.com";
    
    String str2 = str1.intern();
   
    System.out.println(str2);
    
    System.out.println("Is str1 equal to str2 ? = " + (str1 == str2));
  }
}

The code above generates the following result.