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.

Related Topics:

String handling in java with example.
String Comparison in java with example.
String concatenation in java with example.
Substring in java with example.
How to write Immutable class in java with example.
toString() method in java with example.
Commonly used methods of String Class with example.
charAt(int index) String function in java with example.
startsWith(String prefix) and endsWith(String suffix) String functions in java.
indexOf(String str) and lastIndexOf(String str) String functions in java.
toLowerCase() and toUpperCase() String functions in java.
intern() String functions in java with example.
length() String functions in java with example.
trim() String functions in java with example.

 

Please follow and like us:
Posted in Java   

Core Java Tutorial



Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki