For questions in this series, everyone who learns Java should understand. Of course, if just learn Java play, it doesn't matter. If you think that you have surpassed beginners, you don't know these questions, please return your yourself to beginners.
Question 1 I declare what!
String s = "Hello World!";
Many people have done such a thing, but what do we declare? Answer is usually: a string, the content is "Hello World!". Such a blurred answer is usually the root of the concept. If you want an accurate answer, half of people will probably answer the error. This statement declares a reference to the object, named "S", can point to any object of type String, point to "Hello World!" This String type object. This is what truly happened. We didn't declare a String object, we just declare a reference variable that can only point to the String object. So, if you just have the statement, if you run a sentence:
String string = s;
We declare that another reference can only point to String objects, named String, does not have a second object, and string still points to the original object, that is, and s point to the same object.
Question 2
Please refer to the previous one of the posts. http://expert.9cbs.net/expert/topic/2868/2868335.xml?temp=.493664
What is the difference between "==" and Equals methods?
== Operator is specifically used to compare whether the value of the variable is equal. A better understanding is: int A = 10; int b = 10; then a == B will be true. But the place where it is not good is: string a = new string ("foo"); string b = new string ("foo"); then a == b will return false.
According to the previous post, the object variable is actually a reference, and their value is the memory address where the object is located, not the object itself. A and B have used the New operator, which means that two contents of "foo" will be generated in memory, since "two", they are naturally located in different memory addresses. The value of A and B is actually the value of two different memory addresses, so use the "==" operator, and the result will be false. It is true, the objects referred to A and B are "foo", should be "equal", but == operators do not involve the comparison of object content. The comparison of object content is exactly what is doing the equals method.
Look at the equals method of the Object object is implemented: Boolean Equals (Object O) {
Return this == o;
} Object object is used by default == operator. So if your homemade class does not override the equals method, your class uses Equals and use == will get the same result. It can also be seen that Object's Equals method does not meet the target of equals: compares whether the contents of the two objects are equal. Because the answer should be decided by the category of the category, Object left this task to the category of the class.
Look at an extreme class: Class Monster {Private String Content; ... Boolean Equals (Object Another) {Return True;}} I have override the equals method. This implementation will lead to a Monster instance content, and the comparison between them will always return to TRUE.
So when you use the Equals method to determine if the content of the object is equal, please don't want to be of course. Because you may think that the author doesn't think so, the implementation of the class Equals method is mastered by him. If you need to use the equals method, or use any collection of aquary code (Hashset, Hashmap, Hashtable), look at the Java Doc to confirm how the Equals logic of this class is implemented.
Question 3 Reference: Two in the first two http://expert.9cbs.net/expert/topic/2868/2868492.xml?temp://expert.9cbs.net/expert/topic/2868/2868335. XML? Temp = .968487 Question 3String has not changed. Because String is designed to be unality (IMMUTABLE) classes, all objects of it are non-variable objects. Please see the following code: string s = "hello"; s = s "world!"; Whether the object pointed to by S is changed? This conclusion is easy to export from the first conclusion of this series. Let's take a look at what happened. In this code, S is first pointing to a String object, and the content is "Hello", then we have done operations, whether the object points to this change? The answer is not. At this time, S does not point to the original object, and point to another String object, the content is "Hello World!", The object is still in memory, just s this reference variable no longer points to it. Through the above description, we can easily export another conclusion, if you often make a variety of modifications, or unforeseen modifications, use String to represent a string that will cause a large memory overhead. Because the String object cannot be changed after establishment, there is a String object to be represented for each different string. At this time, you should consider using the StringBuffer class, which allows you to modify, not every different strings to generate a new object. And, these two types of object transitions are very easy. At the same time, we can also know that if you want to use the same string, you don't have to have a String every time. For example, we have to initialize a String reference variable called S in the constructor, set it to the initial value, should do this: public class demo {private string s; ... public demo {s = "initial value" } ...} rather than s = new string ("initial value"); the latter will call the constructor each time, generate new objects, low performance, large memory overhead, because String objects can not change, so For the same string as the content, it is ok as long as a String object is indicated. That is, the above-described constructor multiple times creates multiple objects, and their String type attributes s all the same object. The above conclusions are based on the fact that for string of string, if the content is the same, Java believes they represent the same String object. Use the keyword NEW to call the constructor, always create a new object, regardless of whether the content is the same. As for why the String class is designed to be unable class, it is determined by it. In fact, not only String, many classes in the Java Standard Class Bank are not variable. When developing a system, we sometimes need to design non-variable classes to deliver a set of related values, which is also an embodiment of object-oriented ideas. There are some advantages, such as because its object is read-only, so many threaded concurrent access will not have any problems. Of course, there are also some shortcomings, such as each different state, you can represent the representative, which may result in performance.
So the Java Standard Class library also provides a variable version, which is StringBuffer. Question 4 What is the final key? To understand this issue, first understand the concepts set forth in this series of second issues. http://expert.9cbs.net/expert/topic/2868/2868492.xml?temp =.3966181 Final makes the modified variable "unchanged", but because the nature of the object type variable is "reference", make "no "There is also two meanings: the unchanged itself is the same, and the object to which the reference points is constant. Quote itself constant: final stringbuffer a = new stringbuffer ("immutable"); Final StringBuffer B = New StringBuffer ("not immutable"); a = b; // Compile period error reference pointing object constant: Final StringBuffer a = New StringBuffer ("Immutable"); A.Append ("Broken!"); // Compiling It visible, Final is only valid only for reference to the "value" (that is, the memory address of the object it points to), forced Quote can only point to the object of the initial pointing, changing its pointing will result in compile time errors. As for changes in the object it point to, Final is not responsible. This is similar to == Operator: == Operator is only responsible for the "value" of the reference, as for the object content points to this address, == operator regardless of. It is important to understand the FINAL problem. Many program vulnerabilities are based on this --- final can only guarantee the reference to always point to fixed objects, and cannot guarantee that the status of that object is constant. In multi-threaded operation, an object will be shared or modified by multiple threads, and a thread-free modification may cause another thread that uses this object to crash. A wrong solution is to declare it as Final when this object is new, intended to make it "never change". In fact, it is futile. Question 5 Reference http://expert.9cbs.net/expert/topic/2868/2868492.xml?temp=.9501154http://expert.9cbs.net/expert/topic/2868/286835.xml?temp =.968487HTTP : //expert.9cbs.net/expert/topic/2872/2872127.xml? Temp = .4463159 http: //expert.9cbs.net/expert/topic/2873/2873675.xml? Temp = .4053003 Questions 4 How to initialize! This issue discusses the initialization of variables, so let's take a look at which types of variables in Java. 1. Attributes of the class, or the local variable in the method. 3. The parameters of the method for the first variable, the Java virtual machine will automatically initialize. If the initial value is given, the initial value is initialized. If not given, it initializes the default initial value of this type variable. INT type variable default initial value 0float type variable default initial value 0.0FDouble type variable default initial value is 0.0Boolean Type Variable Default initial value for false type variable default initial value 0 (ASCII code) LONG type variable default initial value 0 All object reference type variable default initial value is null, that is, not to any object. Note that the array itself is also an object, so the array references that have not been initialized are also NULLs after automatic initialization. For two different class properties, the Static property is different from the Instance property, and the initialization timing is different.
The instance property is initialized when the instance is created, and the Static property is loaded in class, that is, initialization of the first time, for the creation of the later instance, no initialization. This issue will be discussed in detail in the next series. For the second variable, initialization must be explicitly performed. If you have not initialized it, you try to use it, the compiler will protest. If the initialization statement is in the TRY block or in the IF block, it must also be assigned before the first use. That is to say, put the initialization statement in the condition that the compiler will also protest in only the IF block, because when executed, it may not conform to the judgment conditions behind the IF, so that the initialization statement will not be executed, this A provision must be initialized before the use of local variables. However, if there is an initialization statement in the ELSE block, you can compile, because in any case, there is always at least one initialization statement will be executed, and there is no initialization before use. The same is true for try-catch, if there is only an initialization statement in the TRY block, the complication department passes. If there is also in catch or finally, you can compile. In summary, it is necessary to ensure that local variables must be initialized before use. So, a good practice is to initialize them when they declare them. If you don't know what is good to do, use the above default! In fact, the third variable and the second essentially, are partial variables in the method. However, as a parameter, it is definitely initialized, the incoming value is the initial value, so it does not need to be initialized. What is the problem of 6INSTANCEOF? InstanceOf is a binary operator of Java, and ==,>,