Java String intern() 方法示例。

java 8object oriented programmingprogramming

String 类的 intern()方法返回字符串对象的规范表示。因此,对于任意两个字符串 s 和 t,当且仅当 s.equals(t) 为真时,s.intern() == t.intern() 才为真。

示例

import java.io.*;
public class Test {
   public static void main(String args[]) {
      String Str1 = new String("Welcome to Tutorialspoint.com");
      String Str2 = new String("WELCOME TO SUTORIALSPOINT.COM");
      System.out.print("Canonical representation:" );
      System.out.println(Str1.intern());
      System.out.print("Canonical representation:" );
      System.out.println(Str2.intern());
   }
}

输出

Canonical representation: Welcome to Tutorialspoint.com
Canonical representation: WELCOME TO SUTORIALSPOINT.COM

相关文章