Everyone who learns Java should understand the problems in this series. Of course, it doesn't matter if you just want to learn Java. If you think you are a beginner, but you do not understand these problems, repeat yourself as a beginner. The content is from the classic post of csdn. Question 1: What have I declared! String S = "Hello world! "; Many people have done such a thing, but what have we declared? The answer is usually a string with the content "Hello World !". Fuzzy answers are usually the root cause of unclear concepts. If you want to answer the question accurately, half of them will probably make a wrong answer. This statement declares a reference pointing to an object named "S". It can point to any object of the string type and currently points to "Hello world! "This string type object. This is what actually happens. We didn't declare a String object. We just declared a reference variable that can only point to the string object. Therefore, if we run another sentence after the statement: String string = s; we declare another reference that can only point to the string object, named string, no second object is generated. String still points to the original object, that is, and s points to the same object. Question 2: What is the difference between "=" and equals? = The operator is used to compare whether the values of variables are equal. It is better understood that: int A = 10; int B = 10; then a = B will be true. But what is hard to understand is: string a = new string ("foo"); string B = new string ("foo"); then a = B will return false. According to the previous post, object variables are actually a reference. Their values point to the memory address of the object, not the object itself. Both A and B use the new operator, which means that two strings with content "foo" will be generated in the memory. Since they are "two" strings, they are naturally located at different memory addresses. The values of A and B are actually two different memory address values. Therefore, the result is false when the "=" operator is used. It is true that the content of objects A and B is "foo" and should be "equal", but the = operator does not involve the comparison of object content. The comparison of object content is exactly what the equals method does. Let's take a look at how the equals method of the object is implemented: Boolean equals (Object O) {return this = O;} the object uses the = operator by default. Therefore, if your own class does not overwrite the equals method, you will get the same result when using equals and =. We can also see that the equals method of the object does not meet the goal that the equals method should achieve: compare whether the content of the two objects is equal. Because the answer should be determined by the class creator, the object leaves this task to the class creator. Take a look at the next extreme class: class monster {private string content;... Boolean equals (Object another) {return true ;}}. I have overwritten the equals method. This implementation will always return true regardless of the content of the monster instance. Therefore, when you use the equals method to determine whether the object content is equal, do not take it for granted. Because you may think they are equal, and the authors of this class do not think so, and the implementation of the equals method of the class is controlled by him. If you need to use the equals method or any hash code-based set (hashset, hashmap, hashtable ), check the Java Doc to see how the equals logic of this class is implemented. Question 3: Have the string changed? No. Because string is designed as an immutable class, all its objects are immutable objects. See the following code: String S = "hello"; S = S + "world! "; Does the object pointed to by s change? It is easy to draw this conclusion from the first article in this series. Let's see what happened. In this Code, s originally points to a string object with the content "hello". Then we perform the + operation on S. Does the object s points to change? The answer is no. At this time, s points to another string object instead of the original object, and the content is "Hello world! ", The original object still exists in the memory, but the reference variable s no longer points to it. Through the above description, we can easily draw another conclusion, if you often make various modifications to the string, or make unforeseen modifications, therefore, using string to represent strings will cause a large memory overhead. Because the string object cannot be changed after it is created, a string object is required for each different string. In this case, you should consider using the stringbuffer class, which allows modification, instead of generating a new object for each different string. In addition, the conversion between the two types of objects is very easy. At the same time, we can also know that if you want to use a string with the same content, you do not have to create a new string each time. For example, we need to initialize a string reference variable named S in the constructor and set it as the initial value. We should do this: public class demo {private string s ;... public demo {S = "Initial Value ";}...} instead of S = new string ("Initial Value"); the latter calls the constructor every time to generate new objects. The performance is low and the memory overhead is large and meaningless, because the string object cannot be changed, only one string object can be used to represent strings with the same content. That is to say, the constructor above is called multiple times to create multiple objects, and their string type attribute s points to the same object. The above conclusion is based on the fact that for string constants, if the content is the same, Java considers them to represent the same string object. Using the new keyword to call the constructor always creates a new object, regardless of whether the content is the same or not. The reason for designing a string class as an immutable class is determined by its purpose. In fact, classes in many java standard library are not variable. When developing a system, we sometimes need to design immutable classes to pass a set of related values, which is also the embodiment of Object-oriented thinking. The immutable class has some advantages. For example, because its object is read-only, concurrent multi-thread access will not have any problems. Of course, there are also some shortcomings. For example, each State must be represented by an object, which may cause performance problems. Therefore, the java standard library also provides a variable version, namely, stringbuffer. Question 4: What are the final keywords modified? Final makes the modified variable "unchanged", but because the object type variable is essentially a "Reference", it also has two meanings: The reference itself remains unchanged, and the referenced object. The reference itself remains unchanged: Final stringbuffer A = new stringbuffer ("immutable"); Final stringbuffer B = new stringbuffer ("not immutable"); A = B; // The objects referenced by the compilation errors remain unchanged: Final stringbuffer A = new stringbuffer ("immutable");. append ("broken! "); // The compilation is visible. Final is only valid for the referenced" value "(that is, the memory address of the object it points, it forces the reference to point only to the object that is initially pointed to, changing its direction will lead to compilation errors. Final is not responsible for the changes to the objects it points. This is similar to the = Operator: = Operator is only responsible for the equal value of the referenced "value". As to whether the content of the object pointed to by this address is equal, the = operator does not matter. Understanding final issues has important meanings. Many program vulnerabilities are based on this ---- final can only ensure that the reference always points to a fixed object, but cannot ensure that the state of that object remains unchanged. In multi-threaded operations, an object will be shared or modified by multiple threads. Unintentional modifications made by one thread to the object may cause another thread to crash. The solution to an error is to declare the object as final when it is created, so that it is "never changed ". In fact, it is futile. Question 5: How to initialize it! This topic discusses variable initialization, so let's first look at the types of variables in Java. 1. Class Attribute, or value range 2. Local variables in the method 3. For the first variable, the Java Virtual Machine will automatically initialize it. If an initial value is given, the initialization is the initial value. If not, initialize it as the default initial value of the type variable. Int type variable default initial value is 0 Float Type Variable default initial value is 0.0f double type variable default initial value is 0.0 Boolean Type Variable default initial value is false char type variable default initial value is 0 (ASCII code) the default initial value of long type variables is 0. All object reference type variables default initial value is null, that is, no point to any object. Note that the array itself is also an object, so the value of the uninitialized array reference after automatic Initialization is also null. For two different class attributes, static attributes and instance attributes, the initialization time is different. The instance attribute is initialized when an instance is created. The static attribute is initialized when the class is loaded, that is, the class is used for the first time. The subsequent instance creation is not initialized again. This issue will be discussed in detail in future series. The second type of variables must be explicitly initialized. If you try to use it before initialization, the compiler will protest. If the initialization statement is in the try block or if block, it must be assigned a value before it is used for the first time. That is to say, the compiler will protest when placing the initialization statement in the condition judgment statement that only contains the if block, because the execution may not meet the judgment condition after the if block, in this way, the initialization statement will not be executed, which violates the requirements that must be initialized before local variables are used. However, if there is an initialization statement in the else block, you can compile it, because in any case, at least one initialization statement will be executed and will not be initialized before use. The same applies to try-catch. If there is an initialization statement only in the try block, the compilation department will pass. If it is in catch or finally, it can be compiled. In short, make sure that the local variables are initialized before use. Therefore, a good practice is to initialize them when declaring them. If you don't know what the values will be, use the default values above! In fact, the third and second variables are essentially the same. They are all local variables in the method. But as a parameter, it must have been initialized. The input value is the initial value, so Initialization is not required. Question 6: What is instanceof? Instanceof is a binary operator of Java. It is the same class as =, >,<. Because it is composed of letters, it is also a reserved keyword of Java. It is used to test whether the object on the left is an instance of the class on the right of the object and return boolean data. For example: String S = "I am an object! "; Boolean isobject = s instanceof object; we declare a String object reference, point to a string object, and then use instancof to test whether the object it points to is an instance of the object class, obviously, this is true, so true is returned, that is, the value of isobject is true. Instanceof is useful. For example, we have written a bill processing system, which has three classes: public class Bill {// omitting details} public class phonebill extends Bill {// omitting details} public class gasbill extends Bill {// omitting details} has a method in the handler, accept a bill type object and calculate the amount. Assume that the two billing calculation methods are different, and the incoming Bill object may be either of the two, so you must use instanceof to judge: Public double calculate (bill) {If (Bill instanceof phonebill) {// calculate the telephone bill} If (Bill instanceof gasbill) {// calculate the gas bill }...} in this way, two subclass classes can be processed in one way. However, this approach is generally considered to be failing to take advantage of object-oriented polymorphism. In fact, the above functional requirements can be fully implemented by using method overloading, which is an appropriate approach for object-oriented development and avoids returning to the structured programming mode. You only need to provide two methods with the same name and return value, and accept the different parameter types: Public double calculate (phonebill Bill) {// calculate the telephone bill} public double calculate (gasbill bill) {// calculate the gas bill}. Therefore, using instanceof is not recommended in most cases, so we should make good use of polymorphism. ******** * ***************** Java direction and learning method Java is divided ), j2se (desktop application development), J2EE (Web enterprise applications), so Java is not a standalone version, but an object-oriented language. We recommend that you study the java system in this way: * Stage 1: Java basics, including Java syntax, object-oriented features, common APIs, and collection frameworks; * Stage 2: Java interface programming, including AWT, event mechanism, swing, this part can also be skipped and can be used again. * Stage 3: Java API: input and output, multithreading, network programming, reflection annotation, etc., the essence of Java; * Stage 4: Database SQL basics, including addition, deletion, modification, and query operations and multi-table queries; * Stage 5: JDBC programming: including JDBC principles, although Hibernate is much more convenient than JDBC, The JDBC technology is still in use. The JDBC idea is particularly important. * Stage 6: JDBC has a deep understanding of advanced features: including database connection pools, stored procedures, triggers, and CRM ideas. * stage 7: HTML language learning, including HTML tags, form tags, and CSS. This is the basis for Web application development. * stage 8: javascript scripting language, including ja Vascript syntax and object are described in the following two aspects: * stage 9: Dom programming, including Dom principles, common DOM elements, and important Dom programming ideas; * stage 10: servlet development is an important step in Java development, including the installation and use of XML, Tomcat server, simple understanding of HTTP protocol, and Servlet API. This is the basis of Java Web development. * Stage 1: JSP development: JSP syntax and tags, custom tags, El and jstl libraries, and MVC three-layer architecture design patterns. * Stage 2: Ajax development: Ajax principles, request Response Processing, Ajax Development Library; * Stage 2: Lightweight Framework, learning of Struts framework, one of the three frameworks, has since entered the essence of Java Web development, including struts architecture and various components, tag library and scalability learning; * Stage 1: Hibernate framework learning, one of the three frameworks, including retrieval ing technology, multi-Table query technology, cache technology, and performance optimization; * Stage 2: Spring framework learning. One of the three major frameworks includes IOC, AOP, datasource, transaction, SSH integration, and JPA integration. * Finally, some java technologies are available, including ejb3.0 and so on, you can choose to learn, compared with the three lightweight frameworks, EJB is a well-deserved heavyweight.