Java - String intern() 方法



描述

Java String intern() 方法用於檢索當前字串物件的規範表示形式。簡而言之,intern() 方法用於在堆記憶體中建立字串的精確副本,並將其儲存在字串常量池中。池是 Java 堆記憶體中一個特殊的儲存空間,字串字面量可以儲存在此處。

規範表示形式表示特定型別資源的值可以用多種方式描述或表示,其中一種方式被選為首選的規範形式。

注意 - 可以透過測試其規範形式的相等性來輕鬆測試兩個物件的相等性。

語法

以下是Java String intern() 方法的語法 -

public String intern()

引數

  • 它不接受任何引數。

返回值

此方法返回字串物件的規範表示形式。

建立當前字串副本示例

如果給定的字串不為空,則 intern() 方法會在堆記憶體中建立當前字串物件的相同副本。

在下面的程式中,我們使用值“TutorialsPoint”例項化字串類。使用intern() 方法,我們嘗試在堆記憶體中建立當前字串的精確副本。

package com.tutorialspoint;

public class Intern {
   public static void main(String[] args) {
      
      //instantiate the string class
      String str = new String("TutorialsPoint");
      System.out.println("The given string is: " + str);
      
      //using the intern() method
      String str1 = str.intern();
      System.out.println("The canonical representation of the current string : " + str1);
   }
}

輸出

執行上述程式後,將產生以下結果 -

The given string is: TutorialsPoint
The canonical representation of the current string: TutorialsPoint

建立當前字串副本時出現空指標異常示例

如果給定的字串具有空值,則 intern() 方法會丟擲NullPointerException

在下面的示例中,我們使用空值建立一個字串字面量。使用intern() 方法,我們嘗試檢索當前字串物件的規範表示形式。

package com.tutorialspoint;

public class Intern {
   public static void main(String[] args) {
      try {
         
         //create string literal
         String str = null;
         System.out.println("The given string is: " + str);
         
         //using the intern() method
         String str1 = str.intern();
         System.out.println("The canonical representation of the current : " + str1);
      } catch(NullPointerException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

以下是上述程式的輸出 -

The given string is: null
java.lang.NullPointerException: Cannot invoke "String.intern()" because "str" is null
	at com.tutorialspoint.String.Intern.main(Intern.java:9)
Exception: java.lang.NullPointerException: Cannot invoke "String.intern()" because "str" is null

檢查建立當前字串副本時的相等性示例

使用 intern() 方法,我們可以根據字串物件的圓錐形來檢查它們的相等性。

在此程式中,我們使用值“Hello”建立字串類的物件。然後,使用intern() 方法,我們嘗試檢索字串物件的規範表示形式,並使用equals() 方法進行比較。

package com.tutorialspoint;

public class Intern {
   public static void main(String[] args) {
      
      // create an object of the string class
      String str = new String("Hello");
      System.out.println("The given string is: " + str);
      
      // using intern() method
      String str2 = str.intern();
      System.out.println("The canonical representation of the string object: " + str2);
      
      //using the conditional statement
      if (str.equals(str2)) {
         System.out.println("The string objects are equal based on their canonical forms");
      } else {
         System.out.println("The string objects are not equal based on their canonical forms");
      }
   }
}

輸出

上述程式產生以下輸出 -

The given string is: Hello
The canonical representation of the string object: Hello
The string objects are equal based on their canonical forms
java_lang_string.htm
廣告