Java 字串 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

更新於: 26-2月-2020

138 瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.