Java Virtual Machine Learning notes

Source: Internet
Author: User
Tags call back float double

1. Compilation sequence:
Compiler Virtual Machine
Java source file *. Java -------> bytecode *. Class ------> Class Loader ---> execution engine

A. Class file can only contain one class or interface. Therefore, the number of classes defined in the. Java file and the number of. class files generated during compilation (internal classes are not counted ).

2. Java programs can access the underlying system in either of the following ways:
(1). the Java program calls the Java API to call the local method and access the underlying system, regardless of the platform.
(2). Use a Java program to directly call local methods to access the underlying system related to the platform.
The local method is the method provided by the operating system.

3. Class Loader:
Load the bytecode *. Class and Java API bytecode compiled by the Java compiler to the method area.

Java has two types of loaders:
(1). Launch Class Loader: The system is unique and is part of a virtual machine. It is written in a specific language (similar to the Virtual Machine layer Language) and loaded in the default mode. It is mainly used to load core class libraries.

.
(2). User-defined class loaders: there can be any number of loaders, which can be compiled in Java and are part of a Java application. They can be compiled into bytecode and loaded by virtual machines.

A loader loads a class and all the classes called by this class to connect them and form a namespace. Each Class Loader corresponds to a namespace.

That is, the principle of namespace in Java.

The class loaders are arranged in line, from bottom to top, and start class loaders at the top. Except the start class loader, other class loaders are instantiated by users to load different classes. When you want to load

Class, the loader at the bottom tries to give the class to the parent loader for loading, and the parent class tries to give it to its parent class for loading until the class loader is started. If the parent loader cannot

To load the sub-class loader. The sub-class loader can load the remaining parts until the bottom of the sub-class. For example:
Loader A, B, C, D, E, F, start
A ---> B ---> C ---> D ---> E ---> f ---> Start
When a class Fun needs to be loaded, it will continue to the top to start the class loader. If the boot loader cannot load fun, hand it to F for loading, F for loading, and

The remainder is handed over to E, and then it continues like this.

As mentioned above, classes loaded by each class loader form a runtime package. classes in the same runtime package can access each other, but cannot access classes outside the package.

4. Virtual Machine lifecycle:
Every Java program has its own virtual machine instance, which is generated and extinct with the emergence and extinction of the program.

5. Memory Structure During Java program running:
The program space is divided into the method area, heap, java stack, and local method stack.

(1) Information about the class data stored in the method area includes:
Basic information:
A fully qualified name for each class.
Fully qualified names of direct superclasses of each class (type conversion can be constrained ).
Whether the class is a class or an interface.
This type of access modifier.
List of fully qualified names of the direct superinterface.

Detailed information about each loaded class:
Runtime constant pool: stores all constants used by this type (direct constants and symbolic references to other types, fields, and methods). They are accessed through indexes in the form of arrays and are called externally.

It is a bridge between class and object type. It is the runtime representation of the constant pool of class files (bytecode. (There is also a static constant pool in the bytecode file ).
Field Information: The information (name, type, modifier) of each field declared in the class ).
Method information: information about each method declared in the class (name, return type, parameter type, modifier, method bytecode, and exception table ).
Static variables
To Class classloader reference: refers to the class loader of this class.
Reference to class: the VM creates a class instance for each loaded type to represent the loaded class.

(2) Heap stores all generated objects and instance variables of objects.

(3) The java stack stores the call status of local methods (including parameters, local variables, intermediate results, and so on) in the form of frames ). Every time a method is called, The Method Frame of the method should be pushed

The java stack becomes the current method frame. When the call ends (return), the frame is displayed. When the compiler compiles the original code into a bytecode (. Class), it has locally changed the methods of various types.

Quantity, the size of the operand stack is determined and placed in the bytecode, and loaded into the method area along with the class. When a method is called, access the class information in the method area to obtain the local variables and operands.

Stack size.

Java stack frames (I .e. method frames) are composed of local variable areas, operand stacks, and frame data areas.

The local variable area is an array of words. Each array element corresponds to a local variable value. When a method is called, the local variables of the method are grouped into an array and accessed through indexes.

. If it is a non-static method, add an implicit reference parameter This, which points to the object that calls this method. The static method does not have the this parameter. Therefore, the object cannot call static

Method.

The operand stack is also an array, but it is accessed through stack operations. The so-called operands are the data operated by commands. If A = B + C is required for parameter operations

The number of pressure stacks, such as B and C, are pushed out by operation commands and executed. Here, the iadd command pops up and adds B and C, then press it into the operand stack (a series

The result is then popped up by the I _storex command and saved to the local variable area array pointed to by index x (here index x points to local variable ).
The virtual machine uses the operand stack as the workspace.

Parse the constant pool and handle exceptions in the frame data area.

(4) local method Stack: it is related to the language of the local method called. For example, if a C language method is called, it is a C stack. The local method can call back the Java method.
If a Java method calls a local method, the VM runs the local method. In the virtual machine view, running this local method is to execute this Java method. If the local method throws an exception,

The Virtual Machine considers this Java method to throw an exception.

(5) When executing a program, find the loaded class in the method area through the object reference. If no loaded class is found, find the bytecode (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 speed up access.

(6) Therefore, it can be roughly expressed:
Method Area: The storage class includes various information about the interface, and the bytecode is loaded here.
Java stack: stores information about the called method. The method frame is pushed to the java stack only when the method is called.
Heap: stores object information, including instance variables of objects, but not object methods. The method frame is pushed to the java stack only when the object method is called.

6. Java data type:
Value Type:
Float double
Integer type: byte, short, Int, long, char (INT and char can be exchanged ).
Reference Type: class type, interface type, array type.

7. Reference Type of Java:
References and pointers.
A reference represents a referenced object. It only represents a referenced object, does not occupy memory, and cannot be modified. If the referenced variable does not reference an object, the referenced variable is null.
The address where the pointer stores the object. It is a variable that can be modified. Like other variables, it occupies memory.

8. Method Area
All threads share the method area, but to ensure thread security, 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 a time.

9. Class ID:
Because a program can load the same class multiple times and the class can exist in different namespaces (it can be loaded by different loaders ), therefore, you must add the identifier of the loader that loads this class.

To uniquely identify a class.

10. Object
The object instance variables are stored in the heap, and the object symbol reference is in the constant pool, method Attribute Table, and other possible places. You can access the instance data of the object and create the object through object reference.

Class data. Object references point to objects in the heap.
There are two types of instance structures. For details, refer to page 98.

Dynamic binding is required when the object method is called. That is, the method to be called cannot be determined based on the object, but the method to be called is determined based on the class data of the object. At this time,

You also need to refer to the class data through object reference.
Dynamic binding is bound at runtime rather than during compilation.

11. Array
Arrays are also class objects. Arrays with the same type and dimension belong to the same class (regardless of the length, only dimension is viewed ). The array length belongs to the object instance. Multi-dimensional arrays are also one-dimensional arrays. Example 2

A dimension array is a one-dimensional array. Each element of this one-dimensional array is an array reference. Arrays are also stored in the heap as common objects.
The array name is an array reference, and the array content is accessed through the index, that is, the array number.

12. Exception
A reference to the exception table for this method is saved in the frame data area of the java stack frame. The exception table records the byte code (*. Class) of the method protected by the catch clause (that is,

Bytecode ). When a method throws an exception, the VM searches for a matched catch clause in the corresponding exception table and gives control to the Code in the catch clause.

13. Java execution engine
Platform independence is implemented. The operand stack in the Java method frame is the center, and the local variable array is treated as the CPU register. Each operation of a data item needs to press the operator stack, and then return to the local

Variable Area. The Java virtual machine requires strong type conversion, that is, low precision can be implicitly converted to high precision, and high precision must be forcibly converted to low precision.

14. threads
A thread is an execution body in a process.
Each thread must comply with the object lock, thread wait and notification.
An object is locked to access object resources that are mutually exclusive to threads. Waiting and notification follow reasonable Thread Scheduling for the same purpose. Java objects are locked through instruction sets, same as inheritance

The wait (), Y (), and notifyall () Methods of the object class are used to wait for and notify. When a thread calls the wait () method of an object, the thread is blocked and added to the line of the object.

In the thread blocking queue, the thread in the blocking queue can be awakened only when another thread calls the notification method of the same object.

15. Constant pool
Constant pools are used to store various types of information, including various types of direct constants, and symbol references for other types, fields, and methods.
Constant pools are divided into two types: constant pools stored in. Class bytecode and runtime pools stored in the method area.
A constant pool appears in the form of an entry (similar to an interrupt vector table). Each entry points to a table and stores constant information in the table. However, the corresponding table can be determined from the flag of the entry of the constant pool.

Constant type stored in.
The constant pool entry starts with a flag indicating the type of the constant. Each entry corresponds to a table ending with the symbol _ INFO, which stores the constant compression form.
In addition to directly storing constants, the constant pool also contains the following symbol references:
Class and interface.
Field name and Descriptor (this descriptor is a string indicating the field type. Fields are class variables or instance variables of a class or interface ).
Method Name and Descriptor (this descriptor indicates the type, parameter type, quantity, and sequence of the method returned ).

During running, the VM uses the full qualified name and method of the constant pool and field descriptor to establish the relationship between classes and classes.

The constant pool is only a set of references and descriptors and does not accept any value assignment operation.

All object creation, method, and class variable calls must obtain information from the constant pool, but instance variable calls are obtained from the heap. (Conjecture)

The symbolic reference is used by the virtual machine to obtain the specific address after parsing.

Constant pool resolution replaces symbol references in the constant pool with direct references.

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

Resolve the various symbol references, links between classes, and classes in the code to a constant pool, which is called a dynamic connection.

16. Constant Pool Structure
A constant pool is composed of many units. Each unit is like an entry (Entry | content). When accessing a constant pool unit, you can find the entry through the index and then access its content. But sometimes the unit content is

It may be a constant pool entry (such as a constant pool unit of a class or interface. The entry contains the symbolic reference of this class, that is, constant_class_info, And the content points to

Constant_utf8_info constant pool unit, which stores the full qualified names of the class ). Direct constants such as int and float are the constant values in the content.

17. Structure of the Method Area
The method area stores all information about types and interfaces. The method area includes:

Constant pool: stores direct constants of the type and all fields, methods, and symbol references of other types (only references, not specific information ).

Field Information: all declared fields (including field names, types, modifiers ).

Method information: all defined methods (including method name, return type, modifier, method bytecode, method stack frame size, and method exception ).

Class variable information: The Virtual Machine allocates space for all class variables in the method area. Subsequent initialization and assignment operations are also carried out in the method area to share the information for all class instances.

To speed up access, the virtual machine sets a method table for each non-abstract class in the method area. This table is an array and each element is a direct reference of a method. When the class object is called

Method, search in the method table (the abstract class does not have an instance, so you do not need to call the method, so there is no method table ).

18. Heap
Heap Storage Class instances and arrays (including instance variables that point to class data references in the corresponding method area ).

19. An 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 ";
}
}
Then a = B returns false, c = D returns true.
Because: = compares whether the two sides are the same object.

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

And:
String c = "hello"
String d = "hello"
Create a string-class instance hello, then create two strings to reference the variables C and D, and then let both C and D point to the starting Hello instance. Therefore, C and D point 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: 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.