Deep understanding of Java Virtual Machine-Chapter 1, deep understanding of Java Virtual Machine

Source: Internet
Author: User

Deep understanding of Java Virtual Machine-Chapter 1, deep understanding of Java Virtual Machine

Chapter 2 Virtual Machine bytecode execution engine

 

8.2 runtime stack frame structure

Stack Frame is a data structure used to support virtual machines for method calling and method execution.

Each stack frame includes a local variable table, an operand stack, a dynamic connection, a method return address, and some additional information.

In the active thread, only the Stack Frame at the top of the Stack is valid. It is called the Current Stack Frame of the Current Stack Frame. The method associated with this Stack Frame is called the Current method.

  Local variable table:

Local Variable Table is a storage space for Variable values. It is used to store method parameters and Local variables defined in the method.

When the Java program is compiled as a Class file, the maximum capacity of the local variable table to be allocated by the method is determined in the Code attribute of the method.

The local variable table capacity is measured in the smallest unit of slot.

During method execution, the virtual machine uses the local variable table to pass the parameter value to the Parameter Variable list.

If the instance method is executed (non-static method), the Slot of the 0th-bit index in the local variable table is used by default to pass the reference of the object instance to which the method belongs, you can use the keyword "this" in the method to access this implicit parameter. Other parameters are arranged in the order of parameters, occupying the local variable slot starting from 1.

  Operand Stack:

Operand Stack is also called an operation Stack. It is a post-In First-Out (LIFO) Stack.

When a method is just started to be executed, the method's operand stack is empty. during the execution of the method, various bytecode commands will write and extract content to the operand stack, that is, the inbound and outbound operations.

For example, the byte code command iadd of integer addition has stored two int-type data in the two elements closest to the top of the stack in the Operation stack at runtime. When this command is executed, the two int values are output from the stack and added to the stack.

Dynamic connection:

Each stack frame contains a reference pointing to the method to which the stack frame belongs in the runtime pool. This reference is held to support dynamic connections during method calling.

Method return address:

After a method is executed, you can add the method in the following two ways:

1. The execution engine Exits normally when it encounters the bytecode command returned by any method

2. An exception occurs during method execution, and the exception is not handled in the method body.

Additional information:

8.3 method call

The only task in the method call phase is to determine the version of the called method (that is, the method called)

Resolution:

The target method in all method calls is a symbolic reference in the constant pool in the Class, which converts some of the symbolic references into direct references. It mainly includes static methods and private methods.

DISPATCH:

The dispatch call process is the most basic embodiment of revealing polymorphism features.

1. Static allocation

Human man = new Man ();

When a VM is overloaded, the parameter's static type (Human) instead of the actual type (Man) is used as the final parameter.

The virtual opportunity is automatically converted or boxed according to the type. The variable length parameter has the lowest priority for heavy loading.

2. Dynamic Allocation

It is closely related to override, another important embodiment of polymorphism. When determining whether to call methods in the parent class or override methods in the subclass, the override methods in different subclasses are called based on different child classes instantiated by the parent class.

3. single and multi-assignment

The receiver of the method and the parameters of the method are collectively referred to as the parcel of the method. A single assignment selects the target method based on the number of parcels. A multi-assignment selects the target method based on more than one parcel.

Java1.6 is a language for static multi-assignment and dynamic single-assignment.

4. Implementation of Dynamic VM allocation

Because dynamic distribution is very frequent, a virtual table is created for the method area of the class.

The virtual method table stores the actual entry addresses of each method. If the child class does not override the parent class method, the entry is consistent.

If the subclass is overwritten, the address in the subclass method will be replaced with the entry address of the implementation version of the subclass.

 

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.