Share on Google+Share on Google+

String intern()

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

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.print("Standard representation:" );

Output : After compiling and executing the above program

Download SourceCode



Posted on: June 11, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: String intern()  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image