Thinking in Java study notes - bis

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43442524/article/details/100077118

Initialization and cleanup

With the development of the computer revolution, "unsafe" programming approach has gradually become one of the main cause of costly programming, the initialization and cleanup It involves two security issues.

Many errors c program are derived from the programmer forgets to initialize a variable. Cleanup is a special problem, when finished using an element, it you will not have any effect, so it is easy to forget it. As a result, this element has been the possession of resources will not be released, resulting in resources (especially memory) exhaustion.

Java used in the constructor , a special method which is called automatically when you create an object, and provides additional "garbage collector." For memory resources are no longer used, the garbage collector can be automatically released.

Ensure initialized with constructors

Each class can be assumed to have defined a write initialize()method.

The method of the kinds of questions before you use the name of its object should first callinitialize()

In Java, by providing a configuration, a designer can ensure that each class of objects are initialized. When you create an object, if it has the class constructor, Java will automatically call the appropriate constructor before the operation target user has the ability to ensure the initialized.

How to name

Constructor takes the name of the same class.

class Rock {
    Rock() {
        System.out.print("Rock ");
    }
}
public class SimpleConstructor {
    public static void main(String[] args) {
        for(int i = 0; i < 10; i++){
            new Rock();
        }
    }
}
输出:
    Rock Rock Rock Rock Rock Rock Rock Rock Rock Rock

Now, when you create an object:

new Rock();

It will allocate storage space for the object and call the appropriate constructor.

We do not accept any argument constructor called the default constructor . Like other methods, constructors can form with parameters to specify how to create objects.

class Rock2 {
    Rock2(int i) {
        System.out.print("Rock " + i + " ");
    }
}
public class SimpleConstructor {
    public static void main(String[] args) {
        for(int i = 0; i < 6; i++){
            new Rock2(i);
        }
    }
}
输出:
    Rock 0 Rock 1 Rock 2 Rock 3 Rock 4 Rock 5

With the form of construction parameters, the parameters can be provided during the actual object is initialized. For example, assume there is a class constructor Tree, which takes an integer variable to indicate a high number of

Tree t = new Tree(12);

If Tree(int)a Treeclass is the only constructor, then the compiler will not allow you to create in any other way Treeobjects.

Constructor helps reduce errors and make the code easier to read. In Java, "initialization" and "Create" bound together, the two can not be separated

Constructor is a special type of method, because it does not return to the same value.

This empty return value (void) significantly different. For empty return value, despite what the method itself does not automatically return, but still choose to have it return something else.

The constructor does not return anything, you have no choice (new expression does return a reference to the new object, but the structure itself and there is no return value). If the constructor has a return value, and allow people to choose the type of return, it is bound to have to let the compiler know how to deal with this return value.

Guess you like

Origin blog.csdn.net/qq_43442524/article/details/100077118
Recommended