Java String intern() 方法示例。


String 類的 intern() 方法為字串物件返回一個規範表示形式。因此,對於任意兩個字串 s 和 t,在且僅在 s.equals(t) 為 true 時,s.intern() == t.intern() 為 true。

示例

即時演示

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

更新於:2020-02-26

138 次檢視

開啟你的 職業生涯

完成這門課程獲得認證

開始
廣告
© . All rights reserved.