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 {
// w w w.  j ava 2 s  . com
  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.





















Home »
  Java Tutorial »
    java.lang »




Boolean
Byte
Character
Class
Double
Enum
Float
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runnable
Runtime
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable