String intern()

String intern() method returns canonical form representation of a string object.

String intern()

String intern() method returns canonical form representation of a string object.

String intern()

String intern()

In this section you will learn about intern() method of String class in java.

Description : We are going to use intern() method, intern() method returns a standard form representation of a string object. Basically an intern string is the one that has an entry in the global string pool. And if the string is not in global string pool then it will be added. If the pool already contains a string equal to this String object as determined by the equals(Object) method then on invoking the intern method the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
If two string a and b, a.intern()==b.intern() is true only if a.equals(b) is true. As shown in example below we have created two string . Then we have used  intern() method that we used in println() method to print the canonical form representation of string object.

Syntax : public string intern()

Example :

public class Intern 
          {
		   public static void main(String args[]){
			   
			         String Str1 = "Welcome Rose india";
			         String Str2 ="WELCOME TO ROSE INDIA";

			         System.out.print("standard representation:" );
			         System.out.println(Str1.intern());

			         System.out.print("Standard representation:" );
			         System.out.println(Str2.intern());
			      }
			   }

Output : After compiling and executing the above program

Download SourceCode