Deep Java Virtual machine JVM class load Initialization learning Notes

Source: Internet
Author: User

1. The role of ClassLoader, in general, is to load and load the compiled class into machine memory, in order to provide prerequisites for subsequent execution of the program.

2. Considerations arising from a program:

In the middle of the wind, the teacher in his video gave us a program, claiming to be all the world's Java programmers will make mistakes.

The general conclusion is that a=1,b=1. The reason given is that both A and B are static variables, and both A and B are added 1 when the constructor is called. The answer is 1. But the answer is a=1,b=0 after it's finished running.

Why, the 3 sentence is nothing more than static variable declaration, initialization, the value of the change and the Order of the Declaration of the relationship? Isn't Java object-oriented? It is also related to the structured language, the order. This is directly related to the principle of the Java Virtual machine JVM loading class.

3. How classes work in the JVM

To use a Java class to work for yourself, you have to go through the following steps

1): Class load load: From bytecode binary files ——. class file loads classes into memory, thus reaching a migration of classes from the hard disk to memory, all programs must be loaded into memory to work. Place the in-memory class in the method area of the run-time data area, and then create a Java.lang.Class object in the heap to encapsulate the data structure of the method area. This is the time to show that everything is the object of all things, to do anything must have an object. Is the bottom of the ground is chicken egg, or egg raw chicken? The final product of a class load is a Java.lang.Class object in the heap.

2): Connection: The connection is divided into the following small steps

Validation: For security reasons, verify that the bytecode in memory conforms to the specifications of the JVM, the structure specification of the class, the semantic check, the legality of the bytecode operation, This is to prevent users from creating an illegal Xx.class file to work, or a version of the JVM conflict, such as the JDK6 under the compiled class (which contains the annotation attribute of the classes), is not run under the JDK1.4 JVM.

Prepare: Allocates the static variables of the class to the memory space and initializes the default values. (The object hasn't been generated yet, so there's no instance variable at this time.)

Parsing: Converting a class's symbolic reference to a direct reference (reserved)

3): Class initialization: Assigns the static variable of the class to the correct initial value, which is the initial value given by the developer's own definition, not the default value.

4. Active use and passive use of classes

The following are considered active use of a class, other cases are considered passive use!

1): Instance object of new class most commonly used by beginners (declaration is not called active use)

2): Read and assign operations to static variables of the class.

3): Call the static method of the class directly.

4): Reflection calls a method of a class.

5): When initializing a subclass of a class, the parent class is also equivalent to being actively called by the program (if the static variable that calls the subclass inherits from the parent class and does not have a replication, then it is equivalent to using only the parent class, unrelated to the subclass, so this time the subclass does not need to initialize the class).

6): Run a class directly from the main function entry.

All JVM implementations (different vendors have different implementations, some say IBM's implementation is better than Sun's) ... They are initialized when the class and interface are first invoked.

5. How classes are loaded

1): Directly loaded in the locally compiled class

2): Network load: Java.net.URLClassLoader can load the class specified by the URL

3): Load class from jar, zip and so on, automatically parse jar file to find class file to load Util class

4): Dynamically compiled from Java source code file into class file

6. Class Loader

Default loader that comes with the JVM

1): Root classloader: Bootstrap, written by C + +, is not available for all Java programs.

2): Extension class loader: written by Java.

3): System class, Application class loader: Written by Java.

User-defined ClassLoader: Java.lang.ClassLoader subclasses that allow users to customize how classes are loaded. Each class contains a reference--getclass () that loads his classloader. getClassLoader (). If NULL is returned, it proves that loading his classloader is the root loader bootstrap.

[Email protected]

, the System class, the application class loader) to load. Java.lang.* like the Rt.jar below the JRE are the default root classloader to load these runtime classes.

7. Explain the preparation of the class connection phase

section, and gives it a default value of 0, and a code like B = 10 does not work at this stage, B is still

The default value is 0.

8. The pointer in this is

Pointers to C + +

9. Look back at that weird code.

is to have a singleton instance based on the static method of the inner class.

This time belongs to the active invocation of the Singleton class.

After the memory starts to load the singleton class

1): Allocate space for all static variables of singleton, assign default values, so at this time, Singleton=null, A=0, b=0. Note that the 0 of B is the default value, not the 0 value that we manually assign to it.

2): After assigning a value to a static variable, the assignment at this time is the value that we manually initialize in the program. At this point Singleton = new Singleton (), the constructor method is called. Construction method inside A=1, B=1. Then follow the sequence down.

A is not assigned and remains intact a=1. B is assigned, b the original value of 1 is covered, b=0. So that's how it ends. Static block static blocks in a class are also executed sequentially from top to bottom.

10. Compile a static variable with constant and non-compile constants

That is, the compiler is very smart, at compile time itself can calculate the 4+4 is 8

, is a fixed number. There is no unknown factor in the inside.

At this point the static block executes, proving that the class is initialized. In the case of a static final variable at compile time. If the client program accesses the static variable of the class at this time, it will initialize the class, so as far as possible the static final variable as far as possible no variables in the 1, otherwise performance will be reduced.

Analysis of ClassLoader

The ClassLoader LoadClass method loads a class that is not part of an active call and does not cause the initialization of the class. The following code block

Does not let the ClassLoader initialize test01. Classdemo, because this does not belong to the active invocation of this class.

ClassLoader's relationship:

Genga Loader--"extension loader--" Application ClassLoader--user-defined class loader

The process of loading a class is first loaded from the root loader, not loaded by the root loader, loaded by the extension ClassLoader, loaded by the application loader, and the application loader if it is not loaded by the custom loader (must inherit from Java.lang. ClassLoader) loaded, if the custom loader is not yet loaded. And there is no special class loader, it will throw classnotfoundexception, the surface of the exception is the class can not be found, in fact, the class load failed, but also can not create class object.

If a class can load successfully at a certain level of classloader, then the loader of this layer is called defining the ClassLoader. Then the class reference generated in this layer class returns to the next layer loader called the initial ClassLoader. Because the load succeeds, it returns a class reference to its service object-that is, the classloader that invokes it. Consider the security of the parent delegate loading mechanism.

5/7

It calls a lot of native methods, that is, using JNI to invoke the underlying C + + code.

12. When a class is loaded, connected, initialized, its life cycle begins, when it represents the class object

The life cycle of a class object ends when it is no longer referenced or is inaccessible. The data in the method area of the class is also unloaded, thus ending the life cycle of the class. The life cycle of a class depends on the life cycle of its class object. Classes loaded by the default loader (root loader, extension loader, System loader) that are brought by the Java virtual machine are never unloaded during the JVM life cycle. So the class object of these classes, which I call the template object of the instance, can always be touched! Classes loaded by the user-defined ClassLoader will be unloaded!

7/7

Difficulties in learning can be deducted: 578024144 for communication to get help can also pay attention to the public number: Javaniuniu get free access to lectures



Deep Java Virtual machine JVM class load Initialization learning Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.