01

Essence when using control panel:

Hello. Java uses javac and then becomes hello. class by running the Java command, in the classloader (load, validate, prepare, parse, initialize, use, unload), into the JVM for the Java VIRTUAL machine, in the JVM has the method area, heap memory, thread stack, local method stack, PC counter.

Class loaders:

  1. Load,
  2. Validation,classWhether the version of the file is compatible with the current oneJavaVirtual machine version, and thenclassThe file must meet the vm specifications.
  3. Ready? What do we need to prepare? Is to initialize a class member to its initial value, wherefinalModified class variables,finalVariables are directly initialized to their values, unlike class members.
  4. Parsing, what is parsing? It’s the resolution of a symbolic reference to a direct reference, which is our variablexxxThis representation becomes a direct reference. What is a direct reference? It’s a memory address, as we often seexxx0203r0eAnd this.
  5. Initialize the values ofstaticThe modified variable orstaticStatic code blocks form constructors in order to initialize variables.
  6. Use,
  7. uninstall

JVM

JVM: method area (holds information about all classes and objects that generate information about all classes through a constant pool), heap memory, thread stack, local method stack, counters.

// In the control panel, decompile javap -c XXX is the output of instructions to decompile bytecode into bytecodeCopy the code
public class Hello{ public Hello(); Code: 0: aload_0 1: invokespecial #1 4: return public static void main(java.lang.String[]); . }Copy the code

I’m going to run it in the JVM, run it on the thread stack, main method, run it on the thread stack and if I get a new object keyword, main declares an object in its own memory (thread stack) (object reference refers to an object opened in the heap), Hello, Hello; If you have a heap in the JVM, you apply for a memory address. That is, you create an object, instance variables, and instance methods that point to the heap from the method area.

public class Hello{ public static void main(String[] args){ Hello hello = new Hello(); // Object declaration and create object}}Copy the code

Class loading into a virtual machine:

Public class Demo static {system.out.println (" static code block "); } {system.out.println (" plain code block "); } public Demo(){system.out.println (" constructor "); } public static void main(String[] args){ new Demo(); }} // result Static code block common code block constructor // Load validation ready parse initialization using unload demo. Java -> demo. class -> Load into virtual machine, class loader (validate class information, element information, version, bytecode, ready, To initialize a symbolic reference to a direct reference, initialize a static variable and a static code block.Copy the code

Steps:

The order of execution in heap memory is to load the instance information and then construct the method.

02

Understanding static Cases

Public class F static {system.out.println ("F static code block "); } {system.out.println ("F plain code block "); } public F(){system.out.println ("F constructor "); Public class extends F static {system.out.println ("S static code block "); } {system.out.println ("S common code block "); } public S(){ super(); // Default system.out.println ("S constructor "); Public class Demo public static void main(String[] args){// Create a subclass object new S(); } // result F static code block S Static code block F Common code block F constructor S Common code block S constructorCopy the code

The program enters the method area in the JVM, and the subclass inherits the parent class. The parent class loads the instance information into the allocated memory, and then executes the constructor. New an object in the heap, new S(); There is a default super() in the constructor of the subclass, load the parent class, if the subclass calls the default super(), and the parent class doesn’t have a constructor with no arguments, but a constructor with arguments, then you add it yourself, in super(XXX).