5. Initialization and cleanup; 5. Initialization and cleanup

Source: Internet
Author: User

5. Initialization and cleanup; 5. Initialization and cleanup

1. When creating an object, if its class has a constructor, Java will automatically call the corresponding constructor before the user has the ability to operate the object, thus ensuring initialization.

 

2. Each overload method must have a unique list of parameter types

If the input data type is smaller than the formal parameter type declared in the method, the actual data type will be upgraded.

If the input data type is large, type conversion is required to narrow the conversion.

 

3. If no constructor exists in the written class, the compiler will automatically create a default constructor for you.

 

4. this keyword can only be used inside a method, indicating reference to the object that calls the method.

This keyword can also be used to pass the current object to other methods.

Use this to call another constructor in one constructor. except within the constructor, the compiler prohibits the calling of constructor in any other method, the constructor call must be placed at the very beginning.

The static method does not have this method, and non-static methods cannot be called within the static method.

 

V. 1. Objects may not be recycled

2. Garbage collection is not equal to "analysis structure"

3. Garbage collection is only related to memory

Java Allows defining a method named finalize () in the class to clear memory: Once the garbage collector is ready to release the storage space occupied by the object, it first calls this method, in addition, the memory occupied by objects will be recycled only when the next garbage collection action occurs, but this method should be used as little as possible.

Java Virtual Machine will adopt an adaptive garbage collection technology, namely stop-copy and Mark-sweep (<Java programming philosophy> P90-91)

 

6. If a static basic type field is not initialized, it will obtain the standard initial value of the basic type. If it is an object reference, its default initialization value is null.

The initialization sequence is static objects first, and then non-static objects. The initialization of static objects is only performed once.

Java allows multiple static initialization actions to be organized into a special "static clause"

 

7. array initialization int [] a1 or int a2 []

The Arrays. toString () method belongs to the java. util standard class library and generates a printable version of an array.

Variable Parameter List: Object [] args or Integer... args

Variable Parameter lists and automatic packaging mechanisms can work together

 

8. Use enum to create an enumeration type

When creating an enum, the compiler automatically adds some useful features. The toString () method is automatically created to display the Instance name. The ordinal () method is used to indicate the Declaration Order of an enum constant.

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.