Chapter 1 object Introduction
1. abstraction Mechanism
All programming languages provide abstract mechanisms. assembly is a small abstraction of underlying machines, and C is an abstraction of assembly languages.
2. Each object has an interface that defines the requests you can send to an object. In the modeling and design process of solving the problem, the object is regarded as a "service provider ".
3. Object creation, use, and lifecycle
C ++ focuses on efficiency. The object's storage space and lifecycle are determined during programming. It is achieved by placing the object in a stack or static storage zone, at the expense of flexibility.
Java creates objects in the heap memory pool and uses the garbage collection mechanism.
4,Single inheritance Structure
All OOP languages except C ++ are inherited by a single program. The ultimate base class of Java is object. Benefit: ensure that all objects have a public interface, and they all belong to the same basic type. Ensure that all objects have certain functions, that is, those functions in the object class. Place all necessary operations (such as garbage collection) used by all objects in the object class.
5,Downward transformation and generic template
Upward transformation is safe, and downward transformation requires forced conversion of types. In C ++, a template mechanism is provided to eliminate the need for downward transformation and make mistakes. in Java, a generic method is provided to solve this problem.
6. Concurrency
Java is built into the thread mechanism, which makes concurrency easy.
7. Persistence (not yet learned)
Declare the object as persistent, and the object will be reborn at the next startup.
Chapter 2 everything is an object
1. manipulate objects with reference
The operated identifier is actually a reference of an object. For example, you can regard the object and object reference as the relationship between the TV and the remote control. You can have a reference, you do not need to associate an object with it. You can also create an object without its reference.
String s; this is a string object reference and does not point to the object.
2. Where to store
Data can be stored in six different places:
1), register: the fastest storage zone. You cannot directly control it. It is allocated by the compiler as needed.
2) STACK: in Ram, this is a fast and effective distribution storage method second to registers. The compiler must know the exact size and lifecycle of all data stored in the stack, to move the stack pointer up or down. This constraint limits the flexibility of the program. Objects are stored in the stack, but objects are not stored in the stack. Separated storage of object and Object Reference Storage
3) Heap: A general memory pool, also in Ram, used to store all Java objects. The advantage of heap is that the compiler does not need to know how many storage areas should be allocated from the heap or how long the stored data will survive in the heap, therefore, heap allocation has great flexibility. When you need to create an object, you only need to use new to write a line of code. When this line of code is executed, it will be automatically allocated in the heap. The flexibility cost is that it takes more time to allocate the heap ratio stack.
4) static storage: refers to the storage in a fixed location and in Ram. The static storage area stores the data that always exists when the program is running and uses the static ID.
5) constant storage: stored in the program code. This is safe because they will never be changed.
6) Non-ram storage: it is completely stored outside the program and is not controlled by the program. It can also exist when the program is not running. Such as stream objects and persistent objects.
Special case: Basic Type
New Stores objects in the heap. It is not very effective for an object, especially small simple variables. Therefore, Java retains the basic type and stores it in the stack.
High-Precision numbers: bigiteger and bigdecimal classes for High-Progress computing.
3. Never destroy objects
Object scope: When you use new to create an object, it can survive beyond the scope. Suppose you have the following code:
{
String S = new string ("abcdefg ");
}
The referenced s disappears at the end of the scope, but the object does not disappear, occupying the memory space. NEVER eliminate objects like C ++. Java provides an automatic garbage collection mechanism.
4. Default Value of basic members
If a member of a class is a basic data type, even if it is not initialized, Java ensures that it obtains a default value (see the default table). Note: When the basic data type is a member of the class, java makes sure that its default value is not suitable for local variables !!! However, it is best to assign an initial value wherever you are.
5. Others
Name visibility: This is especially serious in C. C ++ introduces the concept of namespace. Java uses the package mechanism to manage names.
Class positioning: Import Statement. In addition, Java eliminates the issue of forward references. (The class used is defined after the class used)
Chapter 3 Control Program Flow
1. Object alias Problems
Operation object (Object references assign values to each other): After the two references are referenced and copied, the two references point to the same object. The referenced objects that are assigned the value will be reclaimed from the garbage collection.
In a method call (an object acts as a method parameter): An object acts as a method parameter and can change the originally passed-in object because it passes references.
2. Test object equivalence
= And! = Compare object references. If the object has the same content but different references, they are not equal.
Equals () is used to compare whether the actual content of an object is the same. However, if you write a class of your own, you must reload the equals () method, because equals () is referenced by default, most class libraries implement the equals () method used to compare the content of objects. Therefore, class objects in class libraries can be directly called for comparison, you need to reload equals () to implement this function for your own classes !!!!
3. Operators and expressions
Expression: Boolean-exp? Value0: value1 For example: I <10? I * 100: I * 1000.
4. type conversion
Java allows you to convert any basic data type to another basic data type, except for the boolean type. To convert a class to another class, special methods must be used (strings are special cases ). The object type can only be converted in the class family.
5. Improvement
The maximum data type in the expression determines the Data Type of the result. For example, if long and double are multiplied, the result is of the double type. For char, byte, short, and mixed assignment, type conversion is not required. Even if they perform an increase in type, they will get the same result as the arithmetic operation.
6. Java does not have sizeof
In C/C ++, sizeof is designed for transplantation. Different data types may have different sizes on different machines. The size of all Java Data Types on all machines is the same.
7. Process Control
Java cannot use an array as a Boolean value, which is different from non-zero or true in C/C ++.
The comma operator. The only place in Java that uses the comma operator is in the for loop.
Break forcibly exits the loop. If the remaining statement is not executed, continue stops executing the current iteration and starts the next iteration at the beginning of the returned loop.
While (true) and for (;) are infinite loops.
Label and continue, break: Generally, the continue is returned to the beginning of the innermost loop and continues to run. The continue with the label will reach the position of the tag and re-enter the loop next to the tag; generally, the break is interrupted and jumps out of the current loop. The tagged break is interrupted and jumps out of the loop indicated by the tag.
The only reason why labels are needed in Java is loop nesting, and you want to break or continue from more than one loop.