Java.lang.string class is most familiar with everyone, we write Java programs, very few, don't use String. This article tells how to use String correctly, and content mainly involves initialization, series and comparison.
First we must clear the String class is final type, so you can't inherit this class, you can't modify this class. When we use string, it is very simple, usually string s = "hello", but the Java API provides a constructor to String (String s), so you can use String s = new string ("Hello" For the later future, a String is not recommended because the New operator means a new object will be generated on the HEAP, and if such an operation occurs in a loop, the cost is heavy. For example, for (int i = 0; i <1000; i ) {string s = new string ("hello");} This will create 1000 String type objects, because the String class is final, so this fact Every time you have a new String object. If you use string s = "hello"; then you can achieve multiplexing, why can you be reused, the following will explain.
When we use " " to implement series operation, such as string s = "hello" "world"; actually through the StringBuffer class Append () method implementation, finally returns String to s. If you are interested, you can write a simple example, then use Javap to see how the virtual machine works. When we use the series, we should also pay attention to the string is a final class. If you need multiple series such as: string sql = "xxx"; sql = "xxxx"; sql = "sssss"; then in order to improve efficiency saving space, we should Use StringBuffer to replace " ";
There are two cases of String, one is to use ==, the other is to use the equals () method, pay attention to == is comparison to the object's address, and the equals () method in the string is overwriting the Object class. The method is implemented as a comparison of the content of the String object. So String S1 = New String ("Hello"); String S2 = New String ("Hello"), when we perform the above comparisons for S1 and S2, the former should return false because two different objects are generated using new . The latter should return True because their content is the same, all "Hello". Then we have a string S3 = "Hello"; what should he be in a comparison of S1? The answer is S1 == S3 for False, and Equals' comparison bit True. In fact, the String class is to maintain a String pool. This pool is initialized to empty, when we string x = "Hello", Hello will be put in this pool, when we string y = "Hello" When he first checked if there is a one in the pool and the same object. If you exist, you will return this reference to Y. If you don't exist, you will create one and put it into the pool. This is reused. There is a method in string in () he can put the String object into the pool and return to the object in the pool. If we call INTERN, S1 = S1.Intern (), we will find the results to return TRUE in the "==" judgment. See the example of the following example public class stringtest {
Public static void main (string [] args) {string s1 = "hello"; string s2 = new string ("hello"); string s3 = new string ("Hello"); teststring (S1, S2, S3); S2 = s2.intern (); system.out.println ("after s2.intern"); teststring (S1, S2, S3);