Example of intern() function:--
class Test
{
public static void main(String... args)
{
String s1="Good";
s1=s1+"morning";
System.out.println(s1.intern());
String s2="Goodmorning";
if(s1==s2)
{
System.out.println("both are equal");
}
}
}
/***********************
***********************/
Run this program with jdk6 and jdk7..........
Output:--
JDK6:-->Goodmorning
JDK7:-->Goodmorning
both are equal
Definition of intern() function:--
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) methodthen on invoking the intern method the string from the pool is returned. Otherwise, this String objectis added to the pool and a reference to this String object is returned.
No comments:
Post a Comment