The constructor

You could conceivably define an initialize() method for every class you write. The name of this method reminds you that initialize() should be called first before using its object. However, this means that users must remember to call this method themselves. The two cannot be separated.

The default constructor, also known as the no-argument constructor, creates a “default object”. If you write a class that does not have a constructor, the compiler will automatically create one for you. If you already define a constructor (with or without arguments), the compiler will not automatically create a default constructor for you.

Initialize the

In addition to explicit static initialization, there is a syntax for non-static instance initialization, which initializes the non-static variables of each object. Under overloaded constructors, some operation is guaranteed to occur regardless of which explicit constructor is called.

The compiled code for each class exists in its own separate file, which is loaded only when program code is needed. The code for a class is loaded when it is first used. This usually means that loading occurs when the first object of the class is created, but loading also occurs when a static field or static method is accessed. (The constructor is also a static method, although the static keyword is implicit, so it is more accurate to say that the class is loaded when any of its static members are accessed.)

overloading

Overloading and type promotion do not conflict and can coexist

Why is the basis for overloading independent of the return type? Because the return value of a method call is ignorable and optional on the compiler, the compiler cannot distinguish between them.

{ void f() {} int f() {return 1; } f(); }Copy the code

This keyword

The compiler does some behind-the-scenes work to write code in a simple, object-oriented syntax that says “send a message to an object,” secretly passing a “reference to the object being operated on” to the method as the first argument.

{
    Banana a = new Banana();
    a.peel(1);
    
    // equals to 
    Banana.peel(a, 1);
}
Copy the code

Method calls the same type of method internally, without the need to use this.

Clean up the

Java allows you to define a method called Finalize () ina class. Once the garbage collector is ready to release the storage space occupied by the object, the finalize() method will be called first, and the memory occupied by the object will be actually reclaimed when the next garbage collection action occurs. However, objects in Java are not guaranteed to be garbage collected, so if you want to do some cleanup, you must manually create a common method that performs the cleanup.

The only reason to use a garbage collector is to reclaim memory that your program is no longer using, so for any behavior related to garbage collection (especially finalize() methods), they must also be related to memory machine collection. Because the garbage collector is responsible for releasing all memory that objects occupy, this limits the need for Finalize () to a special case where objects are allocated storage space by some way other than the way objects are created.

The garbage collector is not based on reference counting. For any “live” object, it is guaranteed to eventually trace its references alive in the stack or static store, so if you start with the stack and static store and iterate through all the references, you can find all the “live” objects.

There are a number of additional technologies in the Java VIRTUAL machine to speed things up, especially one related to loader operations called just-in-time compilers, which can translate all or part of a program into native machine code (which is what the Java Virtual machine is supposed to do) to speed things up. When a class needs to be loaded (usually when the first object for that class is created), the compiler finds its.class file and loads the class’s bytecode into memory. At this point, there are two options. One is to have the just-in-time compiler compile all the code, but doing so has two drawbacks: This loading action is scattered throughout the program life cycle, takes more time to add up, and increases the length of executable code (bytecodes are much smaller than native machine code after just-in-time compiler expansion), which causes page scheduling and slows the program down. Another exercise is called a lazy evaluation, mean instant compiler to compile the code only when necessary, so, never be executed code may not be compiled by the JIT, at a new version of the JDK Java HotSport technology is adopted in the similar method, every time the code is executed can do some optimization, so the execution, the more The faster it goes.