Five initialize and clean up the Java programming ideas

With the development of the computer revolution, "unsafe" programming method has been known as one of programming gradually costly alterations.
Initialization and cleanup involves two issues security.

5.1 ensure that the initialization with the constructor

By providing a configuration, a designer can ensure that each class of objects are initialized.
Taking into account the compiler during initialization to automatically call the constructor, the constructor and the class using the same name.
When you create an object, it will allocate storage space for the object and call the appropriate constructor.
Constructor is a special type of method, because it has no return value (void return empty, then any return values).

5.2 Method overloading

The method of the same name in the form of different parameters and methods.

5.2.1 distinguish overloaded methods

Each overloaded method must have a unique list of parameter type. Order of the different parameters have enough difference between the two methods.

5.2.2 involving basic types of heavy-duty

If the incoming actual parameter is less than the type of the formal parameter declared method, actual data type will be lifted.
If the incoming actual parameter type of the formal parameter is greater than the declared method, the type of conversion is performed by narrowing necessary conversion.

5.3 default constructor

The default constructor is no formal parameters - its role is to create a "default object."

5.4 this keyword

Look at the following code:

Banana a=new Banana();
Banana b=new Banana();
a.peel(1);
b.peel(2);

For the above code, send a message to the object, the compiler to do some work behind it. It is the object secretly operated as the first argument to peel ().

Banana.peel(a,1);
Banana.peel(b,1);

this keyword can only use internal methods, it expressed "that object to invoke a method" reference.
this. Keywords for the current object to the other methods are also useful.

5.4.1 calls the constructor in the constructor

I want to call another constructor in a constructor, to use this you can do this.
this can be called a constructor, but you can not call the two.

5.4.2 static meaning

In the internal static methods can not call non-static methods, and vice versa can.

5.5 Cleanup: The End of treatment and garbage collection

Java garbage collector is responsible for reclaiming memory resources useless objects occupy. But there are special circumstances: Suppose your object (not using new) to obtain an area of memory, because the garbage collector only knows to release that memory allocated by new, all it does not know how to release this special memory for the object.
To deal with this, Java allows class named defined in a finalize () method: Once the garbage collector is ready to release the memory occupied by objects, will first call finalize () method, and the next garbage collection action occurs when will really reclaim the memory occupied by the object. But note finalize () is not equal to the destructor.
In C ++, the object will be destroyed, and Java objects are garbage collected, but not always:

  • Objects may not be garbage
  • Garbage collection does not mean "destructor"
5.5.1 finalize () uses what

Garbage collection is only related with the memory.
The only reason to use garbage collector for memory recovery program is not in use.
No matter how the object is created, the garbage collector will be responsible for the release of all the memory occupied by objects.

5.5.2 you must be implemented to clean up

If the Java virtual machine and did not face the situation of running out of memory, it is not a waste of time to perform garbage collection to restore the memory.

5.5.3 End conditions

When an object is no longer of interest - that is, it can be cleaned up, the object should be in a certain state, it occupied memory can be freed safe.

5.5.4 How the garbage collector work

The cost of re-allocated on the heap object is very high, however, the garbage collector object is created for improving the speed, but with significant results.
In some of the Java virtual machine heap realization: It's more like a conveyor belt, did not allocate a new object, it will move forward one space.
Garbage collection when it works, the reclaim space on one side, so that the object side arranged in a compact stack, the stack pointer so that it can be easily moved closer to the beginning of the conveyor belt, a page fault may be avoided. By the garbage collector rearranged objects, realize a high-speed, wireless space model for heap allocation.
Garbage collection mechanism in other systems: technical reference method, a simple but slow rate of garbage collection. Each object has a counter, when an object has a reference to the connection, a reference count is incremented. When a reference goes out of scope or set to null, the reference count is decremented.
For a faster mode: for any living objects, you will be able to ultimately be traced back to their survival in the stack or static storage area of reference.
Java virtual machine uses an adaptive garbage collection.
There is a practice called the stop - copy . Pause the program running, then all live objects copied from the current heap to another heap, they are not copied garbage. When the object is copied to the new stack, holding the stack in the new compact arrangement.
In the program into a stable state, so copying a waste. Some Java virtual machine to check: If no new garbage will be converted to another mode, mark - sweep : From the stack and static storage area, through all the references, and then find all the live objects, whenever it find a live objects, the object will give a mark, this process will not recover any object. Only mark completed when the clean-up operation will start. In the cleaning process, there is no marked objects will be released, will not send any copy action.

5.6 member initialization

Java try to ensure: All traverse before use can be properly initialized. For the method of local variables, Java in the wrong form to implement compile time.
When you define an object reference in the class, if you do not initialize it, this quote will get a special null value.

5.6.1 the definition of member variables in place for its assignment.

5.7 constructor to initialize

At run time, you can call a method or perform some action to determine the initial value. But keep in mind: You can not stop the automatic initialization is performed, it will be called in the constructor.

5.7.1 initialization sequence

Within the class, the sequence determines the order of the variable initialization. Walking variable definition method for defining between them will still be initialized before any method is called.

5.7.2 Static data initialization

No matter how many objects are created, static data occupies only a storage area. Keywords can not be used in static local variable, so it can only act on the field. If a field is static basic type field, and did not initialize it, it will get basic types of standard initial value. If it is an object reference, then its default initial value is null.



public class StaticInitialization {
    static Table table =new Table();
    static Cupboard cupboard=new Cupboard();
    public static void main(String[] args){
        System.out.println("in main");
        new Cupboard();
        new Cupboard();
        table.f2(1);
        cupboard.f3(3);
    }
}

class Bowl{
    Bowl(int marker){
        System.out.println("Bowl("+marker+")");
    }
    void f1(int marker){
        System.out.println("f1("+marker+")");
    }
}

class Table{
    static Bowl bow1=new Bowl(1);
    Table(){
        System.out.println("Table()");
        bowl2.f1(1);
    }
    void f2(int marker){
        System.out.println("f2("+marker+")");
    }
    static Bowl bowl2=new Bowl(2);
}

class Cupboard{
    Bowl bowl3=new Bowl(3);
    static Bowl bowl4=new Bowl(4);
    Cupboard(){
        System.out.println("Cupboard()");
        bowl4.f1(2);
    }
    void f3(int marker){
        System.out.println("f3("+marker+")");
    }
    static Bowl bowl5=new Bowl(5);
}

The initialization sequence is first static object, then "non-static" objects.
Object creation, suppose you have a class called Dog:

  • 1. Even without the static keyword, the constructor is actually a static method explicitly.
  • 2. Load new Dog () to create objects when all actions related to static initialization will be executed, and only the first Executive.
  • 3. When you create an object with new Dog (), will first allocate enough storage space for the Dog object.
  • 4. this storage space will be cleared, which will automatically all the basic types of data objects in the Dog are set to default values, and references were set became null.
  • 5. define all of the initial operation in fields appear.
  • 6. Perform constructor.
5.7.3 Explicit static initialization

Java allows multiple static initialization action organized into a special "static clause."

public class Spon{
static int i;
static{
i=47;
}
}
5.7.4 non-static instance initialization

Java also has a similar syntax is known as instantiated, used to initialize non-static variables for each object.

5.8 array initialization

Arrays are only the same type of package to a target sequence or substantially together with the type of a data sequence identifier name. Array by square brackets subscript operator [] be defined and used.
Use the same array and C #.

5.8.1 variable parameter list

All classes are directly or indirectly inherited from the Object class, you can create a method to Object array as a parameter.

public class VarArgs {

    public static void main(String[] args){
       // printArray1(15,13,16,34,"wer",new Integer[]{1,2,3,4});
        printArray1(new Integer[]{1,2,3,4});
    }

    static void printArray1(Object[] args){
        for (Object obj:args) {
            System.out.println(obj);
        }
    }
}

Java SE5 add new variable parameter syntax. In the variable parameters, the parameters of any type may be used, including primitive types.

 static void printArray(Object... args){
        for (Object obj:args) {
            System.out.println(obj);
        }
    }

    public static void main(String[] args){
        printArray(15,13,16,34,"wer",new Integer[]{1,2,3,4});
        printArray(new Integer[]{1,2,3,4});
    }

The parameters passed to the variable parameter 0 is also possible.

public class VarArgs {
    static void printArray(Object... args){
        for (Object obj:args) {
            System.out.println(obj);
        }
    }

    public static void main(String[] args){
       // printArray1(15,13,16,34,"wer",new Integer[]{1,2,3,4});
       // printArray1(new Integer[]{1,2,3,4});
        printArray();
    }

//    static void printArray1(Object[] args){
//        for (Object obj:args) {
//            System.out.println(obj);
//        }
//    }
    //printArray1(15,13,16,34,"wer",new Integer[]{1,2,3,4});
}

5.9 Enumeration

public class SimpleEnumUse {
    public static void main(String[] args){
        Spiciness howHot=Spiciness.MEDIUM;
        for (Spiciness spic:Spiciness.values()) {
            System.out.println(spic+":"+spic.ordinal());
        }

    }
}
 enum Spiciness{
    NOT,MILD,RT,MEDIUM,HOT,FLAMING
}

Guess you like

Origin www.cnblogs.com/Tan-sir/p/11228595.html