Intern () Method:
Public String Intern ()
JDK source code such as:
Returns a normalized representation of a string object.
An initially empty string pool, which is privately maintained by the class string.
When the Intern method is called, if the pool already contains a string equal to this string object (the object is determined by the Equals (object) method), the
The string in the pool is returned. Otherwise, this string object is added to the pool, and a reference to this string object is returned.
It follows for any two strings s and T, and s.intern () = = T.intern () is true only if and only if S.equals (t) is true.
All literal strings and string-assignment expressions are internal.
Return:
A string with the same content as this string, but it is guaranteed to come from a string pool.
Although calling the Intern method in the output has little effect, the background method actually does a series of actions and actions.
"AB" is returned when calling the "AB" Intern () method, but this method first checks to see if there is a string "AB" in the string pool.
Returns a reference to the string if it exists, otherwise the string is added to the string pool, and the reference to the string is returned.
For example:
public class Str2{public static void Test () {String a= "a"; String b=a+ "B"; String c= "AB"; String D=new string (b); System.out.println (B==C); System.out.println (D==C); System.out.println (C==d.intern ()); System.out.println (B.intern () ==d.intern ());} public static void Main (String []args) {new STR2 (). Test ();}}
Java Intern () method