"Java Programming Thought" (Second Edition) Chapter 4: Initialization and Cleaning

xiaoxiao2021-03-06  14

My notes

"The first letter of each function is lowercase" This programming style does not apply to the constructor, because the name of the constructor must completely match the Class name

In Java (and C ), the constructor is another reason for the "function name must be overloaded". Each overload function must have a unique quoter column, not the return value as an overloaded benchmark, because sometimes we perform function calls in F (), but at this time There is no information on the value, and the compiler will be helpless.

The Default constructor will not be actively provided by the compiler to C : When you do not define any constructor, the compiler will be provided, and when you provide any constructor, the compiler will not give You provide the default constructor.

Sometimes in order to facilitate our need to call constructor in the constructor. The following example tells us some behavior rules:

// Calling Constructionors with "this" public class flower {int pointalcount = 0; string s = new string ("null"); Flower (int petals) {petalcount = PETALS;} Flower (String ss) {s = ss;} Flower (String S, INT PETALS) {this (Petals); //! This (s); // can't call twice! This.s = s;} Flower () {THIS ("Hi", 47); Print (); // ------------------- 1} void print () {//! this (11); // not inside non-construction! -------- 2} public static void main (string [] args) {Flower X = new flower (); x.print ();}}

This program tells us a few points: 1. Although you can call a constructor by this, you can't call two in the same manner. // I have initially believed that two is to simplify the compiler's inspection of the nested call, but also to maintain the following 2nd point, because 2, then the second is not at its most started. 2. Calling action on another constructor must be placed in the most beginning, otherwise the compiler will issue an error message. // Individual understanding is that the calling constructor will often overthrow the previous set value, that is, the action before calling the constructor will tend to fail, so it is better to call the constructor in the starting place, but there is no need to force execution? ! 3. It can be seen from the 1, 2 of the code, the compiler does not allow you to call the constructor in any function other than the constructor, //, that is, the constructor is only used by the new keyword by the system. But constructor can call the members of the class!

Finalize () The first usage can give you some special processing before garbage collection, because garbage recycling is only recycled, if you have some other resources, it should be released before the object is destroyed, such as erase Drawing images, etc., it should be used. Second usage: When using native function (Native Methods), Free () can be called in FINALIZE to release the memory allocated by the previous malloc () function.

The operation of Garbage Collector depends on two key technologies in Java: single inheritance technology (inherited from Object), the object is allocated from the heap (in HEAP) (not the basic type variable of non-class members), to allocate in the STACK The efficiency considerations) are integrally defined and initialized in Java, and the two cannot be separated from each other.

Members Initialization: Java assurance, the variable will definitely be initialized before being used. When the variable is defined within the function, Java will use the compile period error message to implement its guarantee. If a Class's data member belongs to the basic type, the situation is slightly different. Because all functions can initialize or use the data value, forced users must be unrealistic before use. However, it is also very dangerous to put a meaningful disposition, so the Class data member of each "affiliated basic category" guarantees that there must be an initial value, that is, the compiler setting the default initial value. Of course, we can also easily define the specified value directly in the Class variable. The constructor can be used to perform an initialization operation, at which time the initialization in the class depends on the definition order of the variables in the Class. For the initialization of Static (once and only once) can occur (such as access.) This time is established at this time, at this time, the Static variable is initialized in all Non-Static variables. Since this, the Static object will not be initialized. We can use the Static initialization block or the Non-Static initialization block. It's really good :), or when you declare, = x, otherwise the compiler will give them a default initial value.

Initialization of Array: An array object implies a member of Length. When accessing the data element, Java will be checked by performing a boundary check, and you can't turn off this feature, the performance efficiency of the program is safer, but eliminates the drawback of the C / C array, and completely kill the buffer overflow attack. The living space of the person. It means that there will never be a base error.

my question

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

New Post(0)