intern() String Method in java

String object in the string constant pool is called as String Intern. We can create an exact copy of heap memory string object in string constant pool.

intern():Returns a canonical representation for the string object.

Syntax:

public String intern()

Note: It returns a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.

Example:

StringInternExample.java

/**
 * This program is used to show the use of intern() method.
 * @author w3spoint
 */
class TestString{
	String str1 = "www.w3spoint.com";
	String str2;
 
	/**
	 * This method is used to show the use of intern() method.
	 * @author w3spoint
	 */
	public void internTest(){
		str2 = str1.intern();
		System.out.println(str2);
	}
}
 
public class StringInternExample {
	public static void main(String args[]){
		//creating TestString object
		TestString obj = new TestString();
 
		//method call
		obj.internTest();
	}
}

Output:

www.w3spoint.com

Download this example.
 
Next Topic: length() String functions in java with example.
Previous Topic: toLowerCase() and toUpperCase() String functions in java.

 

Content Protection by DMCA.com
Please Share