Hello guys, good evening everybody, today we’re going to do the liver class loader, wait a minute, you said the liver is the liver, I don’t think the liver, yes, the liver when we learn anything new, we need to know whether our minds are thinking about the liver or not, so we throw out three questions about the soul, to clarify the purpose of our learning.

Soul three asks: Question 1: What is a classloader? Question2: What are the benefits of learning it? Question3: What about the cost of learning?

1. What is a classloader? Basically, it’s used to load classes, just like we used to do with the older DvD player. When you put a CD into the DvD reader, there are always three words on the screen: In the DvD… This is loading, reading the information from the disc, putting it on the TV, imaging it through the kine-tube, easy to understand.

2. What are the benefits of learning it? On the one hand them loader can let us know by what means the JVM is loading of a class, on the other hand encountered when running the program for us can’t find the way to class or provide troubleshooting ideas of reference, can also be a certain type dynamic loading and a jar package, the benefits are obvious, and only actually tasted food, just know how sweet!

3. What about the cost of learning? Learning cost: only need to take a brain, take a computer on the line, time needs about two hours, the evaluation can accept, the benefits are not very obvious, but also can accept, flies again small that is meat, decide liver!

One, the life cycle of a class of Java that object-oriented languages, affirmation is from life, life in our life can have a life cycle, each one of us will experience birth, adulthood and old age, the demise of the process, and also, each class has its own life cycle, the general class of load passes through seven steps, namely the class loading process, Sometimes the interview will ask, boy, you know how good you are at Java, do you know how to load classes?

(1) Class loading process Class loading generally goes through seven steps: 1. Loading 2. Verify 3. Prepare 4. Parse 5. Initialize 6. I saw, surprised, so many steps I also can not remember down ah, must steal a little lazy, today first remember three, then remember: 1. Load, 2,3,4, synthesize a step called linking, and then initialize the class, I thought for a while, can I simplify it a little bit more, enenen, I would have to write a JVM!

(2) Class loading details know the class loading process, then each step is what? How liver ~( ̄▽ ̄)~*? First there is loading: loading is looking for the byte stream, which is basically finding our Class file, which is the raw material for creating the Class, and then there is linking: linking is broken down into three stages: 1. Validation, 2. Preparation, 3. Parsing. The validation phase is to see if the file we loaded is in the correct format and the dependencies are complete. In preparation, memory is allocated for the static fields of the loaded class. Parsing is basically taking our symbolic references and turning them into actual references. When a class is loaded, it does not know the addresses of its methods and fields. When a class is referenced, the Java compiler generates a symbolic reference to locate the required fields and methods. The purpose of the parsing phase is to resolve these symbolic references into actual references. If the symbolic reference refers to an unloaded class, or to fields or methods of an unloaded class, then parsing will trigger the loading of the class (but not necessarily the linking and initialization of the class).

Next is initialization: If we want to initialize a static field, we can use a static block. If we assign a static field that is final, and its type is a basic type or a string, then that field is marked as a ConstantValue by the Java compiler. Its initialization is done directly by the Java virtual machine. Other direct assignments, as well as the code in all static code blocks, are placed in the same method by the Java compiler and named < clinit >.

In simple terms, class initialization is mainly: 1. Assigning values to fields marked as constant values, 2. Execute the < clinit > method. The Java virtual machine uses locks to ensure that the < clinit > method of the class is executed only once.

Once in this step, the class is ready for use and subsequent uninstallation.

Class loaders

What are the class loaders? There are three class loaders in Java: 1. Start the class loader, mainly load the Java core JAR package such as Rt. jar. 2. The application class loader, mainly is to load our own writing Java classes, 3. The extension class loader, but why did have the extension class loader, mainly because we this dependency is more complex, each application may rely on a lot of common jar package, then we can put the jar is extracted, and then into an expansion pack, Use extended class loaders for uniform loading.

In order to ensure that the class is not loaded repeatedly, each class loader will first check whether the parent class loader is loaded. If so, it will return a reference and use it. If neither class is loaded, it will load 2. When dealing with dependencies, it is common for a class to have multiple classes in it, so the current class loader is responsible for loading the class as well as the classes it depends on. 3. Cache loading: Any class is loaded only once. After loading, it is cached in the memory. 4. You can also customize class loaders.

Liver for so long, finally to end, you can go to the happy video, ha ha ha!