String.intern () will actually return the instance that generated the String content for the first time.
The so-called content is "abc", "123" The contents of this string
1.
String S1 = "a"; ([email protected])
So S1 is the first instance of generating content "a" @100, so if
String S1 = "a"; ([email protected])
String s2 = "a"; ([email protected])
Because their content is the same, there is no explicit new, so the same instance is used, and the natural underlying char[] is the same instance
2.
String S1 = "a"; ([email protected]) (char[]@200)
String s2 = new String ("a"); ([email protected]) (char[]@210)
However, it is important to note that, although two String instances are different, their underlying char[] is the same instance
3.
String S1 = "AB"; ([email protected]) (char[]@200)
String s2 = "a";
String s3 = s2 + "B"; ([email protected]) (char[]@210)
Use variables to stitch strings, end string instances differently, char[] instances are also different
4.
String S1 = "AB"; ([email protected]) (char[]@200)
String s2 = "a";
String s3 = s2 + "B"; ([email protected]) (char[]@210)
String i1 = S1.intern (); ([email protected])
String i3 = S3.intern (); ([email protected])
Finally find I1 = = i3, they are the same instance, this is the role of intern, it will return the first instantiation of this string content instance, here is S1, even if s3! = S1, S3.intern () will also return S1, because S1 is the first
5.
string S1 = new String ("AB"); ([email protected])
String s2 = S1.intern (); ([email protected])
S1! = S2 Note that because S1 uses the new string (), it actually generates a string instance when "AB" appears, so string s1 = new String ("AB"); This one-line program generates 2 String instances, although their internal char[] is the same. Eventually, S1.intern () returns the first instance you don't see
About String.intern ()