Review Java programming ideology 4: initialization and cleaning

Source: Internet
Author: User

I recently worked overtime and didn't have time to read it. It took a long time to write this.

Initialization and cleanup

1. Use the constructor to ensure initialization:

1) when creating an object, it allocates storage space for the object and calls the corresponding constructor. This ensures that the object has been properly initialized before you can operate on it. Note: The constructor name must be exactly the same as the class name, so the encoding style of "lowercase letter of each method" is not applicable to the constructor.

2) The constructor has no return value, which is different from the void type returned. Although void does not return anything, it can still choose to let it return something else. The constructor will not return anything. The new expression returns a reference to the new object, but the constructor does not return any value.

2. Method overload:

1) when an object is created, a name is assigned to the space of the object, that is, the object name is the "house number" of the object ". The so-called method is the name of an action.

2) Reload methods are several methods with the same method name and different method parameter lists. The criterion for distinguishing overload methods is that the overload methods must have a unique list of parameters. Different overload methods can be distinguished for different parameter types, numbers, and sequences. However, try not to use different parameter lists to differentiate overload methods.

3. This Keyword:

1) when the code is compiled, the compiler does some background work. It secretly transmits the "Object Reference operated by each method" as the first parameter to each method.

2) If you want to obtain the reference of the current object within the method, you can use the keyword "this. This keyword can only be used inside a method, indicating the reference of "the object that calls the current method. If you call another method of a class within a method, you do not need to use this. You can call it directly by using the method name.

4. Static keywords:

1) The static modifier method or variable can be used without a lifecycle object. That is to say, you do not need to instantiate an object before calling the static method through the object, you can call the static method directly by using the class name, which is also the main purpose of the static method.

2) The static method cannot call non-static methods or attributes internally, but the opposite is acceptable.

5. Cleanup: final processing and garbage collection

1) Java has a garbage collector to recycle memory resources occupied by useless objects, but it only knows to release the space allocated by new.

2) In C ++, objects will be destroyed (through destructor), but in Java, objects are not always garbage collected.

3) garbage collection is only related to the memory. That is to say, the only reason for using the garbage collector is that the recycle program no longer uses the memory.

4) Java tries its best to ensure that all variables (mainly members of the basic data type) can be properly initialized before use. For global variables (class variables), even if they are not explicitly assigned values, the compiler sets the default values for these variables by default. Local variables inside the method must be initialized by yourself; otherwise, the Java compiler reports an error.

5) for an object, if an object reference is defined in the class, if it is not initialized, the reference will get a special value null.

6. constructor initialization:

1) within the class, the sequence of variable definitions determines the initialization sequence. Even if the variable definitions are scattered between the method definitions, they are still initialized before any methods (including constructors) are called.

2) No matter how many objects are created, static data occupies only one storage area. The static keyword cannot be applied to local variables, so it can only apply to fields.

7. array initialization:

1) An Object Sequence or a basic data sequence encapsulated by an identifier name with the same Array knowledge type.

2) Java array count starts from 0 and can use the maximum subscript dozens of length-1.

3) Java Arrays can be declared in two ways: String [] STR; or string STR []; if you use the direct value assignment method (braces) the array must be initialized with braces after the equal sign when the array is declared. The array is created at runtime.

4) You cannot use new to create basic data types.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.