Java String intern()
方法返回字串對象的規範表示。 因此,對於任何兩個字串s
和t
,當且僅當s.equals(t)
為真時並且s.intern()== t.intern()
時才為真。
語法
以下是此方法的語法 -
public String intern()
參數
- 這是一種默認方法,不接受任何參數。
返回值
此方法返回字串對象的規範表示。
示例
import java.io.*;
public class Test {
public static void main(String args[]) {
String Str1 = new String("Welcome to xuhuhu.com");
String Str2 = new String("WELCOME TO KAOPS.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 xuhuhu.com
Canonical representation:WELCOME TO KAOPS.COM
上一篇:
Java String類
下一篇:
Java快速入門