Java Virtual machine

Source: Internet
Author: User

The call and return of each method of a virtual machine is accompanied by the stack frame's stack and stack, and each stack frame contains a reference to the method that the stack frame belongs to in the run-time pool (indicating which method the stack frame is executing), which is held to support dynamic connections in the method invocation. Some of these symbolic references are converted to direct references during the class loading phase or first use (i.e., in the parsing phase of validation, preparation, parsing, such as static methods in classes, private methods, and final decorated methods, which can be explicitly called at compile time without ambiguity). This conversion is called static parsing, and the other part is converted to a direct reference each time it is run, which is called a dynamic connection.

The method returned contains the normal completion exit and the exception completion exit. Either way, after the method exits, to return to the location where the method was called, the program can continue execution, and when returned, some information is saved in the stack frame to help restore the post-call state.

The normal completion exit is that the execution engine encounters any method that returns a bytecode instruction, depending on the return instruction, whether the return value is required and the return value type.

An exception completion is an exception encountered in the execution of the method, and the exception is not processed in the method body (virtual machine internal exception, Athrow byte code instruction generated exception, etc.), this way exit will not give the upper caller any return value.

Method invocation, which is mainly the process of determining the version of the called method, converting the symbolic reference to a direct reference, and determining the entry address of the specific function. All methods stored in the class file are symbolic references in a constant pool, not the entry address of the method in the memory layout of the actual runtime (equivalent to the preceding direct reference). The method invocation has the following four bytecode directives:

A.invokestatic: Calling a static method

B.invokespecial: Invoking instance constructors <init> methods, private methods, and parent class methods

C.invokevirtual: Calling all virtual methods

D.invokeinterface: Invokes an interface method that, at run time, determines an object that implements this interface.

parsing , the premise of parsing is that the method has a deterministic version of the call before the program actually runs, and that the call version of the method is immutable at run time. In other words, the calling target code must be determined when the code is written and the compiler compiles. The method to be resolved generally has the feature of "compile-time known, run-time invariant", and the method that satisfies this requirement is mainly composed of static methods (the same type association) and private methods (externally inaccessible), and they cannot rewrite other versions by inheritance or otherwise. Any method called by the invokestatic and invokespecial directives can determine a unique invocation version in the parsing phase, with static methods, private methods, instance constructors, and parent class methods that conform to this condition. They parse the symbolic reference as a direct reference to the method when the class loads. These methods are called non-virtual methods, other methods are called virtual methods (except the final method, the final method is not overwritten, there are no other versions, and no polymorphic selection is required).

public class test{public static void Test () {System.out.println ("Hello World"); } public      static void Main (string[] args) {         test.test ();             }  }    

The code above makes it easy to determine the result of a method invocation, which is a reference to a direct reference at compile time.

A parse call is a static process that can be fully determined during the parsing phase of a class load, converting all the symbolic references involved into a deterministic direct reference, without delaying the run-time to completion.

Dispatch (Dynamic dispatch and Static Dispatch), the dispatch call may be static or dynamic, and the number of parcels based on the allocation can be divided into single and multiple allocations, which compose static single dispatch, static multi-Dispatch, dynamic single dispatch and dynamic multi-Dispatch.

public class Test5 {Static abstract class Human{}static class extends Human{}static class women extends Human{}public void SayHello (Human guy) {System.out.println ("Hello,guy");} public void SayHello (women guy) {System.out.println ("Hello,lady");} public void SayHello (Mans guy) {System.out.println ("Hello,gentleman");} public static void Main (string[] args) {Human mans = new Man (); Human women = new Women (); Test5 t = new Test5 (); T.sayhello (man); T.sayhello (women);}}

The above code is an example of a method overload, the type human in main is called the static type of the variable man, the actual type of man, the change of the static type only occurs when used, the static type of the variable itself does not change, and the final static type is known at compile time The result of the actual type change can be determined at run time, and the compiler does not know what the actual type of an object is when compiling the program. Two SayHello calls in main, which version is used entirely depends on the number and type of incoming parameters. Although the static type of the parameters in the code is inconsistent with the actual type, the virtual machine (compile time) overload is determined by the static type of the parameter instead of the actual type. and the static type compilation period is known.

All dispatch actions that rely on static types to locate a method's execution version are called static allocations . A typical application is method overloading . Static dispatch occurs during the compilation phase, so determining the static dispatch action is not actually performed by the virtual machine.

Dynamic Dispatch is closely related to the rewriting of methods , see Code:

Package Com.testone;public class Test5 {static abstract class human{protected abstract void SayHello ();} Static class man extends human{@Overrideprotected void SayHello () {//TODO auto-generated method StubSystem.out.println (" Man say hello ");}} Static class women extends human{@Overrideprotected void SayHello () {//TODO auto-generated method StubSystem.out.println ("Women Say Hello");}} public static void Main (string[] args) {Human mans = new Man (); Human women = new Women (); Man.sayhello (); Women.sayhello (); man = new Women (); Man.sayhello ();}}

SayHello is the method to be executed, the SayHello method belongs to the instance method, at the time of the call, the virtual opportunity to pass the method belongs to the object as a parameter, at this time, according to the object's actual type C in the definition of type C to find a method matching SayHello, and to do access checks, By returning the direct reference to the method, the lookup ends, and the illegalaccesserror exception is returned without passing. Otherwise, the inheritance is searched and validated along the parent class of C, and throws an exception if no suitable method is found in the end.

The first step in dynamic dispatch is to determine the actual type of receiver at run time, so when the method is run, it is matched in the actual type of the object according to the method declaration, so that the dynamic dispatch can be performed so that the result is polymorphic.

Single dispatch and multiple dispatch: The parameters of the method receiver and method are collectively referred to as the method's volume, and the single dispatch selects the target method based on one volume, and the multi-Dispatch selects the target method based on more than one volume.

Package Com.testone;public class Test5 {static Class Qq{}static class _360{}public static class Father{public void Hardcho Ice (QQ Arg) {System.out.println ("father choose QQ");} public void Hardchoice (_360 arg) {System.out.println ("father Choose 360");}} public static class Son extends father{@Overridepublic void Hardchoice (QQ arg) {//TODO auto-generated method STUBSYSTEM.O Ut.println ("son choose QQ"); @Overridepublic void Hardchoice (_360 arg) {//TODO auto-generated method StubSystem.out.println ("son choose 360");} public static void Main (string[] args) {Father Father = new Father (); Father son = new son (); Father.hardchoice (New _360 ()); Son.hardchoice (New QQ ());}}

First, the first sentence in main is bold, the actual type is the same as the static type, but the internal method is overloaded, this static allocation, in the process of static allocation, according to the static type and parameter type to determine the method of invocation, is based on the two-volume selection, and thus belong to the multi-Dispatch, so The static assignment of the Java language belongs to the multi-Dispatch type .

Look at the second bold statement, because the actual type and static type are different, and override the parent class method, this takes the dynamic allocation, in the process of dynamic dispatch, because the method signature has been determined, So the parameter type and number of the method can be determined (both the static type and the actual type of the parameter are not affected by the selection of the method), but the method to invoke which object is not determined, which affects the choice of the method, the only factor that can affect the virtual machine selection is the actual type of receiver of this method. Therefore, there is only one parcel as the basis for selection, so the dynamic dispatch of the Java language belongs to the single dispatch type .

Java Virtual machine

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.