The fifth chapter of Java programming thought notes initialization and cleanup

Source: Internet
Author: User

1. Constructors
    • Because an object constructor that creates a class is automatically executed, it is very good to initialize something
2. Method overloading
    • Method names are the same, parameter lists are different.
2.1 Distinguishing overloaded methods
    • Method overloading after different methods is the method signature--parameter type and number (parameter order can be distinguished but generally do not consider order)
2.2 Overloads that involve basic data types
    • range from small to large: byte > Short > Char > int > Long > Float > Double
    • Automatic promotion: The incoming type is less than the declared type, the passed value is promoted to its nearest type, and the method of the type parameter is called
    • Incoming type is greater than declaration, only incoming type casts can be passed in
3. Constructors and their overloads
    • Does not show the default parameterless constructor is written out, the compiler adds a default parameterless constructor by default, but does not add a default parameterless constructor if you write out any constructors.
    • Overloaded constructors are the same as overloaded methods (the constructor is overloaded, the default constructor does not exist, which is to be written out manually to use the default constructor), and the method signature in the constructor is distinguished when the object is created
4.this function

4.1 This can only be used in a method, which represents the object where the method is called.

The overload constructor is called in the 4.1 constructor only in the first sentence of the constructor, and only one constructor can be called, and only the constructor is allowed to call the constructor.

4.2 This (parameter) calls different constructors according to different parameters

4.3 represents the data member THIS.S. S is not a parameter or a local variable

4.4 Calling other methods This.fun ()

4.5 As the return value return this, returns the object that called this method

5. About static

5.1 static method is the method without this

The 5.2 static method cannot call a non-static method directly, and a non-static method can call a static method.

    • It is straightforward to say that a non-static method cannot be called from an object in a static method.  such as static void F () {g ();} F () and g () belong to the same class

5.2. Calling a non-static method must use an object reference to invoke the object reference. f ();

The 5.3 static method does not require an object to be called directly, class. Static method Name

6. Cleanup: End processing and garbage collection

6.1 The garbage collector reclaims only the memory allocated by new

6.2 Objects may not be recycled

6.3 Garbage collection is not equal to the structure analysis

6.4 Garbage collection is only related to memory, as long as the memory is not used up the object will not be recycled, because the garbage collector also consumes memory.

6.5 Finalize () method
    • The Finalize () method is called first when the garbage collector prepares to release object memory. Each class defaults to a Finalize () method, which can be used to validate the object's finalization criteria.
7. Sequence of initialization:

1. Initialize the object memory space to binary 0 (all data members are set to the default value)

2. If the class has a base class, initialize its base class

3. Static members and static domains (who are first initialized, and only a single space in memory, different objects of the same class can be shared)

4.main method

5. Non-static member variable, reference, instance initialization initialized

6. Constructors

Attention

1,2,3,4 in the initialization order, for the class loading process, 5,6 to create the object procedure, only the object is created to execute:

8. Array initialization

8.1 definition Array int[] A1; Array holds data of the same type

8.2 initialization: int[] A1 = {1, 2, 3, 4};

integer[] A1 = {new Integer (2), 3, 4}; Automatic packaging mechanism

integer[] A1 = new integer[] {2, 3, 4}

8.3 int[] A2; a2 = A1; Reference replication, pointing to the same array

8.4 int[] a3 = new Int[5]; Only the array size (index 0-4) is defined, where the data is not assigned and is initialized to 0,null,false by default.

8.5 a3.length () returns the number of array elements 5

8.6 arrays.tostring () printable one-dimensional array

int New int [5];        a[1] = 5;         // [0, 5, 0, 0, 0]
9. Variable parameter list
    • For the parameter type of the incoming method or the number of arguments is unknown
Before SE5
 Public class Arrayss {    publicstaticvoid  main (string[] args) {        // create an array with the largest base class object so that you can pass in different types of parameters        F (newnewnew Double (8.0), "Java    "}); Static void F (object[] s) {};}
SE5
 Public classArrayss { Public Static voidMain (string[] args) {//you do not have to write an array, but the compiler actually creates an array and does not use the automatic wrapper mechanismFNewInteger (20),NewDouble (8.0), "Java"); G (NewInteger (6), 9);//variable parameter list can coexist with automatic packaging mechanismHNewDouble (8.1), 9.2);//Automatic packaging mechanismG1 (9);//Automatic packaging mechanism    }    Static voidF (Object ... s) {System.out.println (S.getclass ());    }; Static voidGint... s)    {System.out.println (S.getclass ());    }; Static voidh (Double ... s) {System.out.println (S.getclass ());    }; Static voidG1 (Integer ... s) {System.out.println (S.getclass ()); };}

Output:

class [Ljava.lang.Object; class [Iclass  [Ljava.lang.Double; class [Ljava.lang.Integer;
10. Enumeration

Public enum Enumname {No, MILD, MEDIUM, hot}

Using an enum requires creating a reference to that type and assigning a value to an instance

Enunname howhot = enunname.hot;

The fifth chapter of Java programming thought notes initialization and cleanup

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.