String is the most important class in Java. This affirmative inference is not what James Gosling, the father of Java, said, but what silent Wang Er said, so you don't have to doubt its accuracy.
There are a lot of interview questions about string, but I always think it's not very interesting to go around theoretical knowledge. For example: string cmower = new string ("silent King 2"); How many objects are defined?
I always feel that asking me such a question is like torturing me: "since your family bought a refrigerator, shouldn't you know the principle of refrigerator refrigeration?"
Besides, why use string cmower = new string ("silent King 2"); Instead of string cmower = "silent King 2";?
I advise interviewers not to dwell on such questions and remember to "apply what they have learned". If theoretical knowledge is always winding, it is really worthless. If you want me to be an interviewer, the question I want to ask is: "how do you usually judge that two strings are equal? Do you use equals() or = =?"
That's all the foreword says. Next, let's explore some practical knowledge points.
Let's take a look at the definition of the string class: it can be found that the string class is of type final, so it cannot be inherited. If a class can be inherited, the immutability mechanism of the class will be destroyed. Because a subclass can override the methods of the parent class and change the member variable value of the parent class, once the subclass appears in the form of the parent class, the class cannot be guaranteed to be immutable. What are the benefits of the immutability of the string class? 1) As the key of HashMap. Because strings are immutable, So it hashes the code when it is created (hash code) is calculated. This means that you don't have to recalculate the hash code of a string every time you use it, which is more efficient and suitable for being used as a key in HashMap. 2) thread safety. The same string object can be shared by multiple threads. If it is accessed frequently, you can omit the synchronization and lock waiting time, so as to improve performance. 3) String constant pool is required. Later. In particular, all methods of the string class do not change the value of the string itself, but return a new object.
In Java, there are two common ways to create strings: cmower uses double quotes and cmowsan uses the new keyword. What is the difference between them? The answer is as follows: for the same string created with double quotation marks, the result is true when using = = judgment, while for the same string created with new keyword, the result is false when using = = judgment. Why? String is used too frequently in Java. In order to avoid generating a large number of string objects in the system, the designer of Java introduced the concept of "string constant pool". When creating a string with double quotation marks, first check whether there are the same string objects in the string constant pool. If so, take the object reference directly from the constant pool; If not, create a new string object, put it into the string constant pool, and return the object reference. That is to say, "silent King 2" is placed in the string constant pool, and the string object references of cmower and cmower1 are the same. The string object created by the new keyword does not involve the string constant pool and is directly placed in the heap. In other words, although cmowsan and cmowsan1 are called silent King three, they are not alone. It is strongly recommended that you do not create string objects in the form of the new keyword.
Because the string is immutable, a new string object will be created when the string is spliced. As we all know, the memory is certain, so the creation of more objects will affect the system performance. StringBuilder is a class provided to solve the problem of too many intermediate objects caused by string splicing. You can add strings to the end of existing sequences through the append () method, which is very efficient. Then someone will have doubts when splicing strings: "do I use the + sign or StringBuilder?" Let's start with a piece of code: how is this code compiled? You can use JAD (Java decompiler) take a look. Do you see the shadow of StringBuilder? Yes, when you use the + sign to splice strings, the java compiler actually uses the StringBuilder class. Can you use the + sign to splice strings at will? Anyway, the java compiler has automatically optimized for us. But that's not the case. Let's look at this paragraph Code: close your eyes and think about what the java compiler will do? The expected result is to create StringBuilder outside the loop. Can the java compiler do as we want? The results of JAD decompilation are as follows: it seems that StringBuilder is created inside the for loop, that is, it will be created 10 times. God, this is not what we expected! We only want StringBuilder to be created once. No way, the java compiler can't do it. We can only rely on ourselves: it is strongly recommended that if it's just the splicing of three or four strings, just use the + operator, Don't think about performance optimization (for example, you're only 100 meters away from your destination. Are you going to take a taxi or walk by yourself?); if you encounter more than four strings or need to use loops to splice, choose StringBuilder. When I was young, I still made such a mistake: when I went, I was at append() Internal use of method + sign! Because of this mistake, I was almost killed by the leader. You have to be careful.