As for the problems in this series, everyone who learns Java should understand. Of course, it doesn't matter if you just learn to play Java. If you think that I have now exceeded the beginner, but do not understand these problems, please return yourself to the beginner team.
Question one: my voice is clear!
String s = "Hello world!";
A lot of people have done this, but what have we declared? The answer is usually: a string with "Hello world!". Such vague answers are often the source of unclear concepts. If you want to answer correctly, half of the people will probably answer the wrong question.
This statement declares a reference to an object named "s" that can point to any object of type string, currently pointing to "Hello world!" The object of this string type. That's what's really going on. We didn't declare a string object, we just declared a reference variable that could only point to a string object. So if, after that sentence, you run another sentence:
string string = S;
We're declaring another reference that can only point to a String object, named String, and no second object is generated, string or point to the original object, that is, and s points to the same object.
Question two: What is the difference between "= =" and the Equals method?
The = = operator is specifically used to compare variable values for equality. The better understanding is that:
Copy Code code as follows:
Then A==b will be true.
But what's not good to understand is:
Copy Code code as follows:
String A=new string ("foo");
String B=new string ("foo");
The a==b will return false.
According to the previous post, object variables are actually references whose values point to the memory address where the object resides, not the object itself. Both A and B use the new operator, which means that there will be two strings in memory that are "Foo", and since they are "two", they naturally reside in different memory addresses. The value of a and B is actually a value of two different memory addresses, so using the "= =" operator will result in false. Admittedly, the objects of A and B are all "foo" and should be "equal", but the = = operator does not involve comparison of object content.
The comparison of object content is exactly what the Equals method does.
Look at how the Equals method of Object objects is implemented:
Copy Code code as follows:
Boolean equals (Object o) {
return this==o;
}
The Object object uses the = = Operator by default. So if your own class doesn't overwrite the Equals method, then your class uses equals and uses = = to get the same result. It can also be seen that the Equals method of object does not achieve the goal that the Equals method should achieve: Compare two object contents for equality. Because the answer should be determined by the creator of the class, object leaves the task to the creator of the class.
Take a look at an extreme class:
Copy Code code as follows:
Class monster{
Private String content;
...
Boolean Equals (Object another) {return true;}
}
I covered the Equals method. This implementation causes the comparison between them to always return true regardless of the monster instance content.
So when you're using the Equals method to determine whether the object's content is equal, don't take it for granted. Because you may think of equality, and the author of this class does not think so, and the implementation of the Equals method of the class is his master. If you need to use the Equals method, or use any collection based on the hash code (hashset,hashmap,hashtable), look at the Java doc to see how the Equals logic for this class is implemented.
Question three: Has the string changed in the end?
No. Because a string is designed as an immutable (immutable) class, all of its objects are immutable objects. Please see the following code:
Copy Code code as follows:
String s = "Hello";
s = s + "world!";
Can the policy that s point to change? From the conclusion of the first article in this series, it is very simple to derive this conclusion. Let's see what the attack is. In this code, s originally pointed to a string policy, the content is "Hello", and then we have a + operation on S, then the policy points to the attack can be changed? The answer is no. At this point, S does not point to the original policy, and pointed to another string policy, the content is "Hello world!", the original policy is still in memory, only s this citation variable no longer point to it.
After the above elucidation, we are very simple to derive another conclusion, if the string is often a variety of corrections, perhaps, the foreseeable correction, then the use of string to represent the string will cause a lot of memory costs. Since string policy is set up and cannot be changed, a string guideline is required for each of the different strings. At this point, you should think about using the StringBuffer class, it promises to fix, not every different string has to generate a new policy. Moreover, these two kinds of policy transformation is very simple.
Together, we can also know that if you want to use the same string of content, you don't need to have a new string each time. For example, to initialize a string citation variable named s in the structure, set it to its initial value, you should do this:
Copy Code code as follows:
public class Demo {
Private String S;
...
Public Demo {
s = "Initial Value";
}
...
}
Rather than
s = new String ("Initial Value");
The latter calls the struct every time, generates new guidelines, has low functionality and memory overhead, and does not make sense, because the string policy does not change, so the string with the same content is only one string guideline to show. It is also said that repeated calls to the above structure to create a number of guidelines, their string type features s all point to the same policy.
The above conclusion is also based on the fact that, with regard to string constants, as with content, Guangzhou Java training thinks they represent the same string policy. And with the keyword new call to the structure, always create a new policy, regardless of whether the content can be the same.
As to why the string class is depicted as not a variable class, it is a useful resolution. In fact, more than String, the classes in many Java canonical class libraries are not variable. In the development of a system, we sometimes need to describe the requirements of the variable class, to pass a set of associated values, which is also a guideline for the performance of thinking. No change class has some advantages, for example, because its policy is read-only, so multithreading concurrent visit will not have any problems. Of course, there are some flaws, for example, each different situation should be a policy to represent, can form functional problems. So the Java Canonical Class Library also provides a variable edition, that is, StringBuffer.