CodesJava

Easy learning with example program codes

intern() String functions in java

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 codesjava
 */
class TestString{
	String str1 = "www.codesjava.com";
	String str2;
 
	/**
	 * This method is used to show the use of intern() method.
	 * @author codesjava
	 */
	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.codesjava.com

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

 

Please follow and like us:
error
Posted in Java   








Copyright © 2019 CodesJava DMCA.com Protection Status