Java String intern()方法

Java String intern()方法返回字符串对象的规范表示。 因此,对于任何两个字符串st,当且仅当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快速入门