Java Virtual machine-virtual machine byte code execution engine

Source: Internet
Author: User

Java Virtual machine-virtual machine byte code execution engine

All Java Virtual machine execution engines are consistent: the input is a bytecode file, processing is the equivalent process of bytecode parsing, the output is the execution results.

Run-time stack frame structure

The data structure used to support the virtual machine for method invocation and method execution is the stack element of the virtual machine stack. each method, from the start of the call to the completion of the process, corresponds to a stack frame in the virtual machine stack in the stack process .

Because the virtual machine stack is thread-private, each thread has its own virtual machine stack, and each VM stack is made up of many stack frames. Each stack frame includes

    • Local variable table
    • Operand stacks
    • Dynamic connection
    • Method return Address
    • Additional additional information

At the top of the stack is called the current stack frame, for the execution engine, only the current stack frame in the active thread is valid, and the method associated with the current stack frame is called the current method .

Local variable table

Used to store the method parameters and local variables defined within the method . The virtual machine uses the local variable table in the way of index positioning, and the capacity of the local variable table is the smallest unit in the variable slot (Variable slot). Local variables do not have a "prep phase" like a class variable and are not given the system's initial value, so be sure to assign a value to the local variable as it is defined.

Operand stacks

Also known as the Operation Stack, when a method starts execution, the operation stack is empty, and as the method executes, various bytecode instructions write to and extract content from the operand stack, that is, the operation of the stack and the stack. This is done by the operand stack, as in the case of arithmetic operations.

Dynamic connection

Each stack frame includes a reference to the method that the stack frame belongs to in the run-time pool, which is held to support dynamic connections during method invocation. The class file has a large number of symbolic references in the constant pool, and the method invocation directives in the bytecode refer to the symbolic reference of the method in the constant pool as the parameter. Some of these symbol references are converted to direct references during the class load phase or first use, which is called static parsing, and the other part is converted to a direct reference during each run, called a dynamic connection .

Method return Address

After the method begins execution, there are only two ways to exit the method:

    • Normal completion exit: The execution engine encounters a bytecode instruction returned by either method;
    • Exception completion exit: An exception was encountered during execution, and exits were not found in this method because of a matching exception handler.

Method exits the process is actually equivalent to the current stack frame out of the stack, the exit may perform the operation is: Restore the upper method of the local variable table and operation stretch, if there is a return value, put the return value into the stack frame of the caller stack, call the PC count value to point to a command after the method call instruction.

Method invocation

Method calls are different from method execution, and the method invocation simply determines which method to invoke, and does not involve a specific running procedure inside the method. All method calls are symbolic references to a constant pool in the class file, and can be used to determine a direct reference to the target method during class loading or even running. In the parsing phase of the class load, a part of the symbolic reference is converted to a direct reference (static parsing), which can be established as a precondition:

    • Method has a deterministic version of the call before the program actually runs
    • And this method cannot be changed during operation

There are two major types of methods to meet the above conditions

    • static method, directly associated with type
    • Private methods, which cannot be accessed externally

The characteristics of these two methods determine that they cannot be overridden by inheritance or otherwise to rewrite other versions, so they are suitable for parsing during the class loading phase.

Java Virtual Machine provides 5 method call bytecode instructions

    • Invokestatic: Calling a static method
    • Invokespecial: Invokes the instance constructor <init> method, private method, and parent class method;
    • Invokevirtual: Calling all virtual methods
    • Invokeinterface: Invoking an interface method, at run time, determines an object that implements this interface;
    • Invokedynamic: The method referenced by the call point qualifier is parsed dynamically at run time before the method is executed.

As long as the method that can be called by the invokestatic and invokespecial instructions can determine the unique invocation version in the parsing phase, there are static methods, private methods, instance constructor parent methods that conform to this condition, and they can resolve the symbolic reference to the direct reference of the method when the class is loaded. These methods are called non-virtual methods, and other methods other than the final method are called virtual methods .

Assigned

The dispatch call may be static or dynamic.

Static Dispatch

Static dispatch : All the dispatch actions that have been performed by static types to locate a method are called static allocations. Static allocation and overloading are closely related.

What are static types, for example, class human, man, and woman, where both man and woman inherit human.

Package exercise; Public classStaticdispatch {Static classHuman {}Static classMansextendshuman{}Static classWomanextendshuman{} Public void SomeMethod(Human Human) {System. out.println("Human"); } Public void SomeMethod(Mans Man) {System. out.println("Man"); } Public void SomeMethod(Woman Woman) {System. out.println("Woman"); } Public Static void Main(string[] args) {Human man =New Mans(); Human woman =New Woman(); Staticdispatch s =New Staticdispatch(); S.SomeMethod(man); S.SomeMethod(woman); }}

In the main method above, the human is called a static type , or a skin type , while man or woman is called the actual type .

Static type changes occur only when used, the static type of the variable itself is not changed, and the final static type is known at compile time; The result of the actual type change is determined at run time, and the compiler does not know what the actual type of an object is when compiling the program.

// 实际类型变化newMannewWoman();// 静态类型变化s.someMethod((Man) man);s.someMethod((Woman) man);

when overloaded, the compiler is judged by the static static type of the parameter rather than by the actual type . So the example above will print two "Human" instead of a print "man" one Printing "Woman".

If you change from main to

s.someMethod((Man)man);s.someMethod((Woman) woman);

The "Man" and "Woman" will be printed separately.

Dynamic Dispatch

Dynamic allocation : Determines the allocation process for the execution version of a method based on the actual type during the run. Dynamic allocations are closely related to overrides (override) in polymorphism.

As an example,

Package exercise; Public classDynamicdispatch {Static Abstract classHuman {protected Abstract void SomeMethod(); }Static classMansextendsHuman {@Override        protected void SomeMethod() {System. out.println("Man"); }    }Static classWomanextendsHuman {@Override        protected void SomeMethod() {System. out.println("Woman"); }    } Public Static void Main(string[] args) {Human man =New Mans(); Human woman =New Woman(); Man.SomeMethod(); Woman.SomeMethod(); Man =New Woman(); Man.SomeMethod(); }}

The example above will print

ManWomanWoman
Single Dispatch and multiple dispatch

The receiver and method parameters of a method are collectively referred to as the number of methods, and depending on how many kinds of parcels the allocation is based on, you can divide the allocation of allocations into multi-Dispatch and single-Dispatch .

If the allocation process should be based on the method of the receiver and according to the method parameters, is the multi-allocation,Java static allocation is a multi-allocation , if the allocation process only a certain amount as the selection basis, the other parcels do not affect the choice of virtual machines, such as the method parameters do not affect the virtual machine selection, The only factor that can affect virtual machine selection is the recipient of this method, which is a single dispatch, and thedynamic dispatch of Java belongs to a single dispatch .

To summarize: At present, Java is a static multi-dispatch, dynamic single-Dispatch language.

by @sunhaiyu

2018.6.16

Java Virtual machine-virtual machine byte code execution engine

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.