A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/lang/string_intern.htm below:

Java - String intern() Method

Java - String intern() Method Description

The Java String intern() method is used to retrieve the canonical representation for the current string object. In short, the intern() method is used to make an exact copy of a String in heap memory and store it in the String constant pool. A pool is a special storage space in Java heap memory where the string literals can be stored.

A canonical representation indicates that values of a specific type of resource can be described or represented in multiple ways, with one of those ways are chosen as the preferred canonical form.

Note − The equality of two objects can easily be tested by testing the equality of their canonical forms.

Syntax

Following is the syntax of the Java String intern() method −

public String intern()
Parameters Return Value

This method returns a canonical representation for the string object.

Creating Copy of Current String Example

If the given string is not null, the intern() method creates the same copy of the current string object in the heap memory.

In the following program, we are instantiating the string class with the value "TutorialsPoint". Using the intern() method, we are trying to create an exact copy of the current string in the heap memory.

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);
   }
}
Output

On executing the above program, it will produce the following result −

The given string is: TutorialsPoint
The canonical representation of the current string: TutorialsPoint
Getting Null Pointer While Creating Copy of Current String Example

If the given string has a null value, the intern() method throws the NullPointerException.

In the following example, we are creating a string literal with the null value. Using the intern() method, we are trying to retrieve the canonical representation of the current string object.

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);
      }
   }
}
Output

Following is the output of the above program −

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
Checking Equality while Creating Copy of Current String Example

Using the intern() method we can check the equality of the string objects based on their conical forms.

In this program, we are creating an object of the string class with the value "Hello". Then, using the intern() method, we are trying to retrieve the canonical representation of the string object, and compare them using the equals() method.

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");
      }
   }
}
Output

The above program, produces the following output −

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4