Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java String-examples String intern()

Related Tutorials


 
 

Share on Google+Share on Google+

String intern()

Advertisement
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.println(Str1.intern());

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

Output : After compiling and executing the above program

Download SourceCode

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  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.

Posted on: June 11, 2013

Related Tutorials

Discuss: String intern()  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com