Chapter 5 - initialization and cleanup

Think in java study notes

pikzas

2019.03.06

Chapter V initialization and cleanup

Think in Java in the contents of this section is preliminary and introduces some grammatical level of content, another book after Zhou Zhiming specific details need to refer to the "in-depth understanding of the Java Virtual Machine"

Knowledge Point

Method overloading the same method name but different argument lists

  • The number of parameters
  • Type of the parameter
  • Order of the parameters

    Overload basic data types of the parameters

    General data type can be converted from a larger type to a smaller type, but some pay attention

    You can see a special case char char type is promoted directly to int.

    Conversely if larger argument data type, and the parameter is a smaller data type. In the time of the call need to display strong turn. It shows that you may have recognized that there may be a loss of data accuracy.

    The place is a cognitive error, depending on the return value can not be overloaded methods. Because the return value is not a method signature.

    Once you manually rewrite the constructor, the compiler will not Yin style add the default constructor for us.

    this keyword refers to the current class to call who is the object of a method, so this keyword may only exist within the method.

    One application of this is used to return the object itself

class Demo{
    int i = 0;
    Demo increment(){
        i++;
        return this;
    }
    void print(){
        System.out.println("i = "+i);
    }
    public static void main(String[] args){
        Demo demo = new Demo();
        demo.increment().increment().increment().print();
    }
}
//output 
//i = 3

Constructor can call another constructor, but a constructor can be called only once at most other constructors, and the calling code must be written in the first line of the constructor, and the method can not be called a constructor.

Your constructor never been performed, the objects are not created successfully, you do not want to engage in something else.

Meaning static keyword

static said that I was class inherent property or method, without relying on the kind of object that can exist. So static property or method declaration, at the time the compiler to load the .class file, which was placed in the memory. So whether you're new out how many objects, I was only one.

How java garbage collector works

  • If the JVM is not the premise of running out of memory, the garbage collector is not going to perform garbage collection.
  • Java garbage collector is stopped adaptive generational - copying (stop-copy) labeled - cleaning (mark-sweep) of the formula

Adaptive systems run at different stages, using different garbage collection policy

The system is stable partition generation, on memory block, while the indicia on which the algebra, the new generation, the old generation, Eden area. . .

Stop - replication system has just started, loading the new objects are more frequent need to allocate memory, memory consolidation. Following the adoption of the stack and static storage area in the object tag to identify the dead objects are not used to garbage collection, and the adjustment of space, then the program will stop.

Mark - sweep for earlier versions of jvm, if the system is stable, it will mark the memory is the same, only the memory clean enough time will occur. Once occurs, the system will stop for a while.

Member initialization

  • Class fields (global variable), if the value is not given, the default basic data types to 0, 0.0, or null, null reference data types give
  • The method of the field (local variable), the initial value is not given, the compiler will report an error.

Initialized global variables - Specify initialization

  • Directly to a specific value
class Demo{
    int i = 123;
}
  • Call methods to assign
class Demo{
    int i = fun(); 
    int j = fun2(i);
    int fun2(int in){
        return 2*in;
    }
    int fun(){
        return 11;
    }
}

Constructor to initialize

Before the constructor initializes members of the default initialization will happen, when the class is assigned to the memory, give all global variables to specify default values.

Chinese think in java fourth edition (94)

class Counter{
    int i;
    Counter(){
        i = 7;
    }
}

We can not stop automatic initialization is performed, it will happen before the constructor is called. I is first set to be 0, then becomes 7. For all primitive types and reference types, the time has been included in the definition of the initial value of the specified variable, such cases are established.
So when we take into account concurrent object only automatic initialization, the constructor has not been called, the object generated at this time is not the complete object.

The initialization sequence

1 to initialize the static object (if the class has been previously instantiated, the object will not be static loaded again)
2 and a non-static object
3 is the last constructor

The actual implementation of the initialization process

1 Find * .class file
2 loaded class file to create a Class object and initialize all static objects
3 new objects, the first thing on the heap to allocate enough memory (static properties are loaded finished)
4 This memory area space set to 0 (the result of automatic initialization, all data is started to zero or null)
(values of all non-static properties have set) 5 performs assignment operations defined in class
6 specifies constructor (the constructor is invoked, may cause the object attributes changes may also result in the parent class is called ...)

Static code block

class Cup{
    static Cpu cup1;
    static Cpu cup2;
    static {
        cup1 = new Cup();
        cup2 = new Cup();
    }
}

Non-static block

class Cup{
    Cpu cup1;
    Cpu cup2;
    {
        cup1 = new Cup();
        cup2 = new Cup();
    }
}

Array initialization

  • int[] arrays;
  • int arrays []; two kinds of methods can be defined.

The default length array has a property indicates that the number of objects within the array, and the array indices start from 0, so the maximum value of length-1

The variable parameter list


class Demo{
    void fun(Object... objs){
        //objs 就是一个数组
    }
}

Therefore, when calling fun (), the argument is an array of objects directly, as fun (new Object [] {1,2,3}). The object may be a string, such as fun (1,2,3), which will be automatically converted into an array as a parameter. Nothing can pass.

The variable parameter list and method overloading due to the conflict between the method can accept a variable parameters, the parameters of what can not, at the same time two methods if the variable argument list, are not passed to the Senate, this time it will burst compiler error.

Enumeration is a class, just add a lot of java default method, it can also be achieved through the interface. But more complex. And realize enumeration is safe


public enum Smile{
    HAHA,XIXI,HEHE
}

Some methods comes

  • ORDINAL () is defined according to the order, counted from 0
  • toString () into literal (not define the order of writing toString method)
  • values ​​() obtained from the literal array composed of
class Demo{
    public static void main(String[] args){
      for(Smile s: Smile.values()){
          System.out.println(s + ". ordinal " + s.ordinal());
      }
    }
}
//output
// HAHA. ordinal 0
// XIXI. ordinal 1
// HEHE. ordinal 2

Enumeration and switch case and with

switch(Smile){
    case HAHA: sout(1111) break;
    case XIXI: sout(2222) break;
    case HEHE: sout(3333) break;
    default: sout(4444);
}

Guess you like

Origin www.cnblogs.com/Pikzas/p/12164323.html