[Learn Notes] Thinking in Java (THE 2nd Edition) Study Note (1)

xiaoxiao2021-03-06  43

Thinking in Java (The 2nd Edition) Study Note

Chapter 1 Objects Getting Started

Alan Kay summarizes the five basic features of SmallTalk. This is the first successful object-oriented programming language and the basics of Java. Through these features, we understand what is the "pure" object-oriented programming method:

(1) Everything is an object. Objects can be imagined into a new variable; it saves data, but it can be required to operate itself. In theory, all conceptual components can be proposed from issues to be resolved, and then express them into an object in the program.

(2) The program is a combination of a large number of objects; through message delivery, each object knows what you should do. In order to issue a request to an object, you need to "send a message" to that object. More specifically, you can imagine the message as a call request, which calls a subroutine or function from the target object.

(3) Each object has its own storage space, which can accommodate other objects. Or, by encapsulating existing objects, new objects can be produced. So, although the concept of object is very simple, it can reach any high complexity in the program.

(4) Each object has a type. Depending on the syntax, each object is a "instance" of a "class". Among them, "class" is a synonym of "type" (Type). A class is the most important feature is "what message can I send it?".

(5) All objects can receive the same message. This is actually a saying that there is something else, you can understand soon. Since an object of the type "circle" is also an object of the type "Shape" (Shape), a circle can fully receive shape messages. This means that the program code will command "shape" to automatically control all objects that meet the "shape", which naturally include "circle". This feature is called "replaceability" of the object, one of the most important concepts of OOP.

Chapter 2 Everything is Object 2.1 Handle Operation Objects

Create a String handle: string s; only the handle created here is not an object. If you send a message to S, an error (running period) is obtained. This is because S is not connected to anything (ie "no TV"). Therefore, a more secure approach is to create a handle, remember to perform initialization, String s = "a";

When you create a handle, we hope that it will connect to the same new object. This is usually used to achieve this with the new keyword. New meaning: "Types me into a new type of these objects." So in the above example, it can be said: string s = new string ("a");

2.2 When the program is running, we'd better save the data to where you have a number. It is important to pay attention to the distribution of memory. Six places can be saved: (1) Register

(2) Stack: Residing in a regular RAM (Random Access Memory) area can obtain direct support for processing through its "stack pointer". This is a very fast, particularly effective data storage method, second only to the register. When you create a program, the Java compiler must accurately know "length" and "existing time" of all data saved in the stack. This is because it must generate the corresponding code to move the pointer up and down. This limitation undoubtedly affects the flexibility of the program, so although some Java data should be saved in the stack - especially the object handle, the Java object does not place it.

(3) Heap: A conventional use of memory pool (also in the RAM area) where Java objects are saved. Unlike the stack, "Memory" or "Heap" The most attractive place is that the compiler does not have to know how much storage space is to be allocated from the heap, and you don't have to know how long the stored data is to stay in the stack. Therefore, greater flexibility is obtained when saving data with a stack. When you create an object, just use the new command to prepare the relevant code. When you perform these code, you will automatically save the data in the stack. Of course, in order to achieve this flexibility, it will inevitably pay a certain price: it will take a longer time (4) static storage when the storage space is allocated: "Static" here "is" located in a fixed position. " "(Although it is also in the RAM), the static stored data will wait any time during operation. The STATIC keyword can be used to indicate a particular element of an object is static. Java objects themselves will never be placed in static storage.

(5) constant storage constant value is usually directly within the program code. This is safe because they will never change. Some constants need to be strictly protected, so they can consider placing them into read-only memory (ROM).

(6) Non-RAM storage If the data is completely independent of a program, the program can still be present when the program is not running, and outside the program's control range. Two of the most important examples are "flow objects" and "fixed objects". For streaming objects, the object will become byte stream, which is usually sent to another machine. For fixed objects, the object is saved in the disk. They still keep their own status unchanged even if the program is aborted. For these types of data stores, a particularly useful technique is that they can exist in other media. Once needed, they can even return them into ordinary, RAM-based objects. Java 1.1 provides support for LightWeight Persistence.

2.3 Scope of the object {String S = New String ("a string");} / * The end point of the scope * / then the handle S disappears at the end of the scope. However, String objects pointing to the String still occupies the memory space. In this code, we have no way to access objects because the only handle pointing to it has exceeded the boundaries of the scope.

2.4 Javadoc Comments

1. @Param format is as follows: @Param Parameter name description, "parameter name" is an identifier within the parameter list, and the "description" represents some explanations that can continue to subsequent lines. Once a new document mark is encountered, it is considered that the previous description ends. Any number of instructions can be used, each parameter.

2. The @return format is as follows: @Return Description where "Description" means the meaning of the return value. It continues to the back of the line.

3. The format of @Exception violation markers is as follows: @Exception The full class name description, "Full Class Name" explicitly specifies the name of a violation class, which is defined in other places. "Description" (also continued to the following line) tells us why this special type of violation will appear in the method call.

4. @DepRecated This is the new feature of Java 1.1. This tag is used to indicate that some old function has been replaced by improved new features. The role of this tag is that users do not have to use a specific function, because this function may be abandoned when the future is revised. If a method is marked as @DepRecated, a warning for the compiler will be received when using this method.

Chapter 3 Control Procedure Process "is like any perceived organisms, the program must be able to manipulate your own world and make judgments and choices during the implementation process."

3.1 Assignment When the value is "assigned", the situation has changed. When an object is operated, we truly operate its handle. So if you assign a value from an object to another object, it is actually copied from one place to another. This means that if "c = D" is used as an object, then C and D will eventually point to that object that is originally pointing only by D. 3.2 == Problem Integer N1 = New Integer (47); Integer N2 = New Integer (47); System.out.Println (N1.Equals (N2)); // The result is False, because of the comparison N1 and N2 Quote (ie, compare points to n1 and n2 pointers)

Each class has a default equals () method, because Object has such a method; all class defaults inherit it, but compare references

转载请注明原文地址:https://www.9cbs.com/read-59635.html

New Post(0)