The intern () method can effectively speed up JVM comparison.
In string comparison, = compares the addresses of two referenced variables.
The equals method (which is rewritten in the string class) compares the actual values of the two variables.
= Simply compare the values of two referenced variables.
The equals method needs to determine whether the values of the two referenced variables are equal in the heap memory based on the addresses of the two referenced variables.
So = is much faster than the equals Method
However, the emergence of the intern () method makes this phenomenon more obvious.
When the intern () method is called, the constant pool is used to determine whether the content pointed to by the referenced variable exists in the constant pool.
If a reference variable exists, direct it to the content in the constant pool without opening the space in the heap memory to store the reference.
Content pointed to by the variable
S4 = (S1 + S2). Intern (); // (S1 + S2) the actual value is "abcabc" Call the intern () method. This method is in the constant pool.
Check whether "abcabc" already exists because the previous string S3 = "abcabc"; the constant pool already exists.
So we direct S4 to this content. So S3 = S4 is true.
That is to say, the intern () method makes the point of the string reference variable more explicit and uses the constant pool to manage the content.
This greatly increases the = comparison speed.
Therefore, the intern () method is used in the comparison of a large number of string variables. It can be used to compare string variables more efficiently. The Intern () method can effectively speed up JVM comparison.
In string comparison, = compares the addresses of two referenced variables.
The equals method (which is rewritten in the string class) compares the actual values of the two variables.
= Simply compare the values of two referenced variables.
The equals method needs to determine whether the values of the two referenced variables are equal in the heap memory based on the addresses of the two referenced variables.
So = is much faster than the equals Method
However, the emergence of the intern () method makes this phenomenon more obvious.
When the intern () method is called, the constant pool is used to determine whether the content pointed to by the referenced variable exists in the constant pool.
If a reference variable exists, direct it to the content in the constant pool without opening the space in the heap memory to store the reference.
Content pointed to by the variable
S4 = (S1 + S2). Intern (); // (S1 + S2) the actual value is "abcabc" Call the intern () method. This method is in the constant pool.
Check whether "abcabc" already exists because the previous string S3 = "abcabc"; the constant pool already exists.
So we direct S4 to this content. So S3 = S4 is true.
That is to say, the intern () method makes the point of the string reference variable more explicit and uses the constant pool to manage the content.
This greatly increases the = comparison speed.
Therefore, the intern () method is used to compare a large number of string variables. It can be used to compare string variables more efficiently and quickly.