Java Virtual Machine

Source: Internet
Author: User

1. in the following cases, the Java Virtual machine will end the life cycle:

1). Implemented the System.exit () method

2). The program is finished executing properly

3). The program encountered an exception or error during execution and terminated abnormally

4). The Java Virtual machine process terminates due to an operating system error


2. loading, connection and initialization of classes:

1). Loading: Finding and loading binary data for a class

2). Connection:

a). Validation: Ensure that the class being loaded is correct

b). Prepare: Allocates memory for static variables of a class and initializes it to a default value

c). Parsing: Converts a symbolic reference in a class to a direct reference. That is, the object address that points directly to the reference, not through the object reference.

3). Initialize: Assigns the correct initial value to the static variable of the class


3. Java programs can be used for classes in two ways: active use and passive use


4. All Java Virtual machine implementations must initialize each class or interface when it is "first active" by a Java program


5. Six types of active use:

1). Create an instance of the class

2). Access static variables for a class or interface, or assign a value to the static variable

3). Calling the static method of the class

4). Reflections (e.g. Class.forName ("ClassName")

5). Initializes a subclass of a class

6). The class that is marked as the startup class when the Java Virtual machine starts (the entry class for the program)


6. In addition to the above six cases, other ways of using Java classes are treated as passive use of the class, without causing the initialization of the class


7. Class loading refers to reading the binary data in the class's. class file into memory, placing it in the method area of the run-time data area, and then creating a Java.lang.Class object in the heap that encapsulates the class in the method area.

Data. Such as:

The method area is a binary data structure for the class, and the class object is in the heap memory, which points to the type binary data structure in the method area.

The binary data is unloaded when there is no class object in the heap that points to the type binary data structure in the method area.


8. How to load the. class file

1). Load directly from the local system

2). Download the. class file over the network

3). Load. class files from archive files such as zip, jar, etc.

4). Extract the. class file from the proprietary database

5). Dynamically compile the Java source files into a. class file


9. The final product loaded by the class is a class object that is located in the heap area. The class object encapsulates the data structure of the classes within the method area, and provides the Java programmer with an interface to access the data structures within the method area


There are two types of class loaders

1). The loader that comes with the Java virtual machine

a). Root class loader (Bootstrap)

b). Extension class loader (Extension)

c). System class Loader (systems)

2). user-defined class loader

a). Subclasses of Java.lang.ClassLoader

b). User can customize how classes are loaded


The JVM specification allows the ClassLoader to preload a class when it is expected to be used, and if it encounters a missing or error in a. class file during a pre-loading process, the ClassLoader must first actively use the class in the program

Error is reported (Linkageerror error) If the class has not been actively used by the program, the ClassLoader will not report an error


after the class is loaded, it enters the connection phase. A connection is the merging of binary data from a class that has been read into memory into the runtime environment of the virtual machine.


contents of the validation of class

1). Structure check of the. class file

2). Semantic check

3). Byte Code Verification

4). Verification of Binary compatibility


in the initialization phase, the Java virtual machine executes the initialization statement of the class, assigning the initial value to the static variables of the class. In a program, there are two ways to initialize a static variable:

1). Initialize at the declaration of a static variable

2). Initializing in a static code block

The Java virtual Opportunities Follow the initialization statements in sequential order in the file to execute them sequentially.


initialization steps for class:

1). If this class has not been loaded and connected, then load and connect first.

2). If the class has a direct parent class and the parent class is not initialized, initialize the immediate parent class first.

3). If there are initialization statements in the class, execute the initialization statements in turn.


When a Java virtual machine Initializes a class, all its parent classes are required to be initialized, but the parent interface that it implements is not initialized. At the same time, when an interface is initialized, its parent interface is not initialized.

Therefore, a parent interface is not initialized because of its sub-interfaces or the initialization of the implementation class. The initialization of the interface is only caused when the program first uses a static variable for a particular interface.


The active use of a class or interface can be considered only if the static variable or static method accessed by the program is indeed defined in the current class or in the current interface. If you use inherited static properties or methods, you will actually only

the parent interface or parent class is initialized.


Call the LoadClass method of the ClassLoader class to load a class that is not actively used on the class and does not cause initialization of the class. only the load, not the initialization, will be initialized only if it is actively used for the first time.


The class loader is used to load classes into a Java virtual machine. Starting from the JDK1.2 version, the class loading process uses the Father delegation mechanism, which can better ensure the security of the Java platform. In this delegation mechanism, in addition to the Java

In addition to the root ClassLoader that the virtual machine comes with, the rest of the ClassLoader has only one parent class loader.


Some of the loaders that are in Java:



In addition to the loader that comes with the above virtual machine, the user can also customize the loader. Java provides an abstract class Java.lang.ClassLoader, and all user-defined class loaders should inherit the ClassLoader class.


What needs to be pointed out is that the parent-child relationship between the loaders actually refers to the wrapper relationship between the loader objects, not the inheritance relationship between the classes. A pair of parent-child loaders may be two instances of the same loader class,

Or maybe not. A parent loader object is wrapped in a child loader object.


The advantages of the father delegation mechanism are that it can improve the security of the software system. Because of this mechanism, the user-defined ClassLoader cannot load reliable classes that should be loaded by the parent loader, thus preventing unreliable and even malicious code

Reliable code that is loaded instead of the parent loader


each class loader has its own namespace, and the namespace consists of classes loaded by the loader and by all the parent loaders. The class's complete name (including the package name of the Class) does not appear in the same namespace for the same two classes;

It is possible that the full name of the class (including the package name of the Class) is the same as two classes in different namespaces.


The class that is loaded by the same class loader is comprised of a runtime package that is part of the same package. Decide whether the two classes belong to the same run-time package, not only to see if their package name is the same, but also to see if the ClassLoader that defines the class is the same.

Only classes that belong to the same run-time package will access each other's classes and class members that are visible to the package (that is, the default access level). Such restrictions can prevent user-defined classes from impersonating the class of the core class library to access the package-visible members of the core class library.


to create a user's own loader, you only need to extend the Java.lang.ClassLoader class and then overwrite its findclass (String name) method. The method returns the corresponding name of the class specified by the parameter.

A reference to the class object.


The namespace relationships of different classloader:

1). The namespace of the child loader contains all of the parent loader's namespaces. Therefore, classes loaded by the child loader can see the classes loaded by the parent loader.

2). Classes loaded by the parent loader cannot see the class that the child loader loads.

3). If there are no direct or indirect parent-child relationships between the two loaders, the classes they load are not visible to each other.


class Unloading: Classes loaded by the class loader that comes with the Java virtual machine are never unloaded during the lifetime of the virtual machine. The Java Virtual machine comes with a class loader that includes a loader, an extension class loader, and a system classloader.

These classloader are always referenced by the Java Virtual machine itself, and these classloader always refer to the class objects of the classes they load. As a result, these class objects are always accessible.


The classes loaded by the user-defined ClassLoader can be unloaded. When a class ends its life cycle, it depends on when it is represented by the class object.

Java Virtual Machine

Related Article

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.