This article is participating in “Java Theme Month – Java Development Practice”, for details: juejin.cn/post/696826…
This is the 10th day of my participation in Gwen Challenge
Method call details
The invocation target must be determined when the program code is written and the compiler compiles. This type of invocation is called parsing.
parsing
* * “in the Java language conform to compile time, the runtime immutable” * * the request method, mainly including the static method and private method two kinds big, the former is directly associated with the type, the latter cannot be accessed outside, which determines the characteristics of two kinds of methods are impossible through inheritance or other ways to rewrite the other version, So they’re all good for parsing during class load.
Static dispatching
See method overloading more often.
- “Human” is the Static Type or Apparent Type of variables. The “Man” is the Actual Type of variables.
- The difference is that the static type changes 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 it compiles the program.
The code defines two variables with the same static type but different actual types, but the virtual machine (or, more accurately, the compiler) uses the static type of the parameter as the basis for reloading, not the actual type.
And the static type is known at compile time, so at compile time the Javac compiler decides which overloaded version to use based on the static type of the parameter, so sayHello (Human) is chosen as the call target. All dispatch actions that rely on static types to locate the version of method execution are called static dispatch.
-
A typical use of static dispatch is method overloading.
-
Static dispatch occurs at compile time, so the action to determine static dispatch is not actually performed by the virtual machine.
Dynamic dispatch
The static types man and woman, both Human variables, perform different actions when calling the sayHello method, and man performs different methods in the two calls. The obvious reason for this is that the actual types of the two variables are different.
In the implementation, the most common means is to create a virtual method table for the class in the method area.
-
The virtual method table stores the actual entry address of each method. If a method is not overridden in a subclass, the address entry in the virtual method table of the subclass is the same as the address entry of the same method in the parent class, pointing to the implementation entry of the parent class.
-
If the method is overridden in a subclass, the address in the subclass’s method table is replaced with the entry address pointing to the subclass’s implementation version.
-
Overrides all methods from Father, so Son’s method table has no arrows pointing to Father data. But neither Son nor Father overrides methods from Object, so all methods inherited from Object in their method tables refer to Object data types.
Stack – based bytecode interpretation execution engine
-
The instruction stream output by the Java compiler is basically a stack-based instruction set architecture. Most of the instructions in the instruction stream are zero-address instructions, which rely on the operand stack to work.
-
Register-based instruction set, the most typical is x86 two address instruction set, said some popular, is now our mainstream PC directly support instruction set architecture, these instructions rely on registers to work.
The simplest example is to compute the result of “1+1” using the two instruction sets separately. A stack-based instruction set would look like this:
iconst_1
iconst_1
iadd
istore_0
Copy the code
After the iconst_1 instructions push the constant 1 onto the stack, the iADD instruction pushes the top two values off the stack, adds them together, and puts the result back on the top of the stack. Finally, istore_0 puts the top value into Slot 0 of the local variable table.
If register-based, the program might look something like this:
Mov eax, 1 add eax, 1Copy the code
The MOV instruction sets the value of the EAX register to 1, and the add instruction increases the value by 1. The result is stored in the EAX register.
-
The main advantage of stack-based instruction sets is portability. Registers are provided directly by the hardware on which the program depends, and registers are inevitably constrained by the hardware.
-
The main disadvantage of stack architecture instruction sets is that they are relatively slow to execute. The instruction set of all major physical machines is a register architecture which also confirms this point from the side.