Java Virtual Machine Learning notes

Source: Internet
Author: User
Tags abstract exception handling execution float double object object variables variable thread
1. Compile Order: Compiler virtual Machine virtual machine Java source file *.java-------> Bytecode *.class------> class Loader---& gt; execution engine

A. class file can contain only one class or interface. So how many classes are defined in the. java file, and how many. class files (internal classes do not count) are generated at compile time.

The program can choose two ways to access the underlying system, which is chosen by the programmer: (1). Call Javaapi via Java program to call the local method, access the underlying system, regardless of the platform. (2). Call the local method directly through the Java program to access the underlying system related to the platform. The local method is the method provided by the operating system.

3. Class Loaders: Loading the Java compiler compiled bytecode *.class and Java API bytecode to the method area.

Java has two kinds of loaders: (1). Start class Loader: System only, part of the virtual machine, written in a specific language (with the virtual Body Layer language) Use the default way to load classes, mainly used to load the core class library

。 (2). user-defined class loader: can have any number of, written in Java, part of the Java application, can be compiled into bytecode, and is loaded by the virtual machine.

A loader loads a class and all classes called by that class, makes them interconnected and forms a namespace (name space), and each class loader corresponds to a namespace.

The principle of namespaces in Java.

Class loaders are aligned in a linear, bottom-up, top-starting class loader. In addition to starting class loaders, other class loaders are instantiated by the user to load different classes. When you want to load a

class, the bottom loader attempts to give the class to the parent loader and the parent class attempts to load the parent class up until the class loader is started. If the parent class loader cannot

Load, then to the subclass loader load, the subclass loads the part that can be loaded, and the remainder to his subclass until the bottom is over. such as: Loader a,b,c,d,e,f, start a--->b--->c--->d--->e--->f the---> Startup when a class fun needs to be loaded, he will go up to the top and start the class loader. If the boot class loader cannot load the fun, it is given to the F load, the F load can load the part, will it

The remainder is given to E, and then it goes on.

As mentioned above, the classes loaded by each class loader during the run form a Run-time package, and classes in the same Run-time package can access each other, but cannot access classes outside the package.

4. Virtual machine life cycle: Each Java program has its own virtual machine instance, with the production and extinction of the program and extinction. Program Runtime memory structure: program space is divided into method area, heap, Java stack, local method stack.

(1) The method area holds the loaded class data information including: basic information: The fully qualified name of each class. The fully qualified name of the direct superclass of each class (a constraint type conversion). Whether the class is a class or an interface. The access modifier for this type. An ordered list of fully qualified names for the direct hyper-interface.

Details of each loaded class: Run a constant pool: All constants used to hold the type (direct constants and symbolic references to other types, fields, methods), which are accessed as an array through the index, are external calls

A bridge that relates to classes and types of objects. It is the run-time representation of a class file (bytecode) constant pool. (There is also a static constant pool, in a bytecode file). Field information: Information (name, type, modifier) for each field declared in the class. Method information: Information about each method declared in the class (name, put back type, parameter type, modifier, byte code of method, and exception table). Static variable to class ClassLoader reference: That is, a reference to the class loader of the class. Reference to Class class: The virtual machine creates a class instance for each loaded type to represent the loaded class.

(2) The heap holds all the generated objects and the instance variables of the objects.

(3) The Java stack holds the invocation state of the local method (including the parameters of the method call, local variables, intermediate results, etc.) in the shape of the frame. Each method is invoked to push the method frame into the

Java stack, which becomes the current method frame. When the call ends (returns), the frame pops up. When the compiler compiles the original code into bytecode (. Class), it has already changed the local size of the various types of methods

Quantity, the operand stack size is determined and placed in the byte code, and the class is loaded into the method area together. When the method is invoked, the local variable and the operand are obtained by accessing the information of the class in the method area

The size of the stack.

Java stack frame (i.e. method frame) is composed of local variable area, operand stack and frame data area.

The local variable is an array of words, with each array element corresponding to the value of a local variable. When a method is invoked, the local variable of the method is formed into an array, and the index is used to access the

。 For non-static methods, add an implied reference parameter this, which points to the object that called the method. Static methods, however, do not have the This parameter. Therefore, the object cannot invoke the static


The operand stack is also an array, but it is accessed through stack operations. The so-called operands are those data which are instructed to manipulate. When a parameter operation is required, such as A=B+C, the parameters that are about to be manipulated are

The number of pressure stacks, such as the B and C stack, and then by the operation instructions will they eject, and perform operations, here by the iadd instruction will b and C pop-up and add, and then into the operand stack (a series of Iadd

Row) and then the i_storex instruction pops the result and saves it to the array of local variables to which the index x points (where the index x points to local variable a). The virtual machine takes the operand stack as the workspace.

The frame data area handles constant pool parsing, exception handling, and so on.

(4) Local method stack: associated with the language of the local method being invoked, such as a C-language method called a C stack. Local methods can recall Java methods. If a Java method invokes a local method, the virtual machine runs the local method. In the case of a virtual machine, running this local method is to execute this Java method, if the local method throws an exception,

The virtual machine thinks that this Java method throws an exception.

(5) When executing a program, look up the loaded class in the method area by reference to the object, and if it is not yet mounted, look for the byte code (class name. Class) and load it into the method area. During execution, the virtual opportunity replaces the object's symbolic reference (that is, the object name) with a direct pointer to increase the speed of the access.

(6) Therefore, the general can be expressed as: Method area: The storage class includes the various information of the interface, the bytecode is loaded here. Java stack: Stores the various information about the invoked method, which is pressed into the Java stack only when the method is invoked. Heap: Stores information about an object, including an object's instance variable, but does not include methods for the object. The method frame is pressed into the Java stack only if the object's method is invoked. data type: Numeric type: Floating-point type: float double integer type: byte,short,int,long,char (int and char can be interchanged). Reference type: Class type, interface type, array type.

Reference type for reference and pointer. A reference represents the referenced object, it is simply a representation of the object, does not occupy memory, and cannot be modified. If the reference variable does not refer to the object, the reference variable =null. The pointer holds the address of the object, which is a variable that can be modified and, like other variables, consumes memory.

8. The method area is shared by all threads, but for thread safety, each class in the method area must be set as a critical resource, that is, a class can only be accessed by one thread at the same time.

9. Class Identity: Because a program can load the same class multiple times and the class can exist in different namespaces (which can be loaded by different loaders), the identity of the loader that loads the class must be added

To uniquely identify a class.

10. Object object instance variables are stored in the heap, and object symbol references are in a constant pool, where the method property sheet may appear. An object's reference allows access to the object's instance data and the creation of the object

The data for the class. Object refers to objects in the heap. The instance structure has two kinds, see Book 98 pages.

Dynamic binding is required when the object's methods are invoked. That is, you cannot determine the method that needs to be invoked based on the object, but instead determine the method that needs to be invoked based on the object's class data. Now

You also need to access the class data through a reference to the object. Dynamic binding is binding at run time, not at compile time.

11. Array arrays are also objects of the class. An array with the same type and number of dimensions belongs to the same class (regardless of the length of the dimension only). The length of an array is part of an object instance. A multidimensional array is also a one-dimensional array. such as two

Dimension array, a one-dimensional array in which each element of the one-dimensional array is a reference to an array. Arrays are stored in the heap as well as normal objects. An array name is a reference to an array, which is accessed by means of an index, an array label.

12. Exceptions hold a reference to the exception table for the method in the frame data area of the Java stack frame. The exception table records the scope of the method's bytecode (*.class) that is protected by a catch clause (that is, in the TRY clause.

byte code). When a method throws an exception, the virtual machine looks for a matching catch clause in the corresponding exception table and hands control to the code in the Catch clause.

The execution engine realizes platform independence, takes the operand stack in the Java method frame as the center, and treats the local variable array as the CPU register. Each operation of a data to press the operator stack, and then return to the local

Variable area. Java virtual machines require strongly typed conversions, where low precision can be implicitly converted to high precision, and high precision must be cast to low precision.

14. A thread thread is present in the process of an executing body. Each thread must adhere to object locking, thread waiting, and notification. Object locks the Access object resource that makes a thread mutually exclusive. The wait and notice is followed by a reasonable scheduling of threads to achieve the same goal. Java objects are locked by instruction set and inherited

The object class waits (), notify (), and Notifyall () method to wait and notify. When a thread invokes the wait () method of an object, the thread is blocked and added to the object's line

Process to block a thread in the queue until another thread calls the notification method of the same object.

15. Constant pool constant pools are used to hold various types of information, including various direct constants of types, and symbolic references to other types, fields, and methods. There are two types of constant pools, Chang stored in the. Class bytecode, and a running constant pool stored in the method area. Chang appear in the form of an entry (similar to the interrupt vector table), where each entry points to a table that stores the information for the constant. But the table can be judged from the flag bit of the entry of the constant pool

The type of constants stored in the The constant pool entry begins with a flag bit indicating the type of the constant. Each entry corresponds to a table that ends with a symbolic _info that holds the compressed form of the constant. The constant pool holds the following symbolic references in addition to the direct constants: the fully qualified name of the class and interface. The field name and descriptor, which is a string indicating the type of the field. A field is a class or instance variable of a class or interface. The method name and descriptor (the descriptor indicates the method return type, parameter type, quantity, order).

At runtime, the virtual machine uses the fully qualified name and method of the constant pool, and the descriptor of the field establishes the relationship between the class and the class.

A constant pool is simply a collection of references and descriptors and does not accept any assignment operations.

All object creation, methods, and invocation of class variables are to get information from the constant pool, but the invocation of instance variables is obtained from the heap. Guess

A symbolic reference is used by a virtual machine after parsing it to obtain a specific address.

Constant pool resolution is to replace a symbolic reference in a constant pool with a direct reference.

When you want to use a method or field of a class, you first find the symbol reference for the method or field from the constant pool, and then parse it to find its physical address.

A constant pool parsing, called a dynamic connection, refers to the various symbolic references, classes, and classes that appear in the code.

16. Constant pool structure constant pool is made up of many ruthless units, each of which is shaped like (the entry | content), accessed through the index when accessing the constant pool cell, and then accesses its contents. But sometimes the contents of the unit are also

It may be a constant pool entry, such as a constant pool unit of a class or interface, where the entry contains a symbolic reference to the class that is Constant_class_info, and the content points to a

A Constant_utf8_info constant pool unit in which the fully qualified name of the class is stored. and direct constants such as Int,float, the content is the value of the constant.

17. The structure method area of the method area stores all information about the type, interface. The method area contains:

Chang: A direct constant of the storage type and all fields, methods, other types of symbolic references (just references, no specific information is stored).

Field information: All declared fields (including field names, types, modifiers).

Method information: All defined methods (including method name, return type, modifier, method byte code, method stack frame size, method exception).

Class variable information: the virtual machine allocates space for all class variables in the method area, and the subsequent initialization, assignment, and so on, is also done in the method area to share for all class instances.

To improve access speed, a virtual machine sets a method table for each Non-abstract class in the method area, which is an array, and each element is a direct reference to a method. When the object of the class calls the

method is searched in the method table (there is no instance of the abstract class, so there is no method table).

18. The heap heap holds instances and arrays of classes (including instance variables that point to references to class data in the corresponding method area).

19. One example class Test{public static void Main (string args[]) {string A=new string ("Hello"); string b=new string ("Hello") ; string c= "Hello"; string d= "Hello";} a==b returns False,c==d returns ture. because: = = Compare whether the two sides are the same object.

First: String A=new string ("Hello") string b=new string ("Hello") A and B respectively create new Hello objects and reference variables, that is, there are two hello in the heap, and their respective references are A and B.

And: String c= "Hello" string d= "Hello" first create a String class instance Hello, then create two strings to reference the variables C and D, and then let C and D all point to the Hello instance that you started to build. So C and D are pointing to the same object.

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: 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.