日期:2014-05-20  浏览次数:20745 次

JVM学习笔记-调用Java方法(Invoking a Java Method)

Invoking a Java Method

?

As mentioned in Chapter 5, "The Java Virtual Machine," the virtual machine creates a new stack frame for each Java (not native) method it invokes. The stack frame contains space for the method's local variables, its operand stack, and any other information required by a particular virtual machine implementation. The size of the local variables and operand stack are calculated at compile-time and placed into the class file, so the virtual machine knows just how much memory will be needed by the method's stack frame. When it invokes a method, it creates a stack frame of the proper size for that method. The virtual machine pushes the new stack frame onto the Java stack.

?

如第5章所述,虚拟机为每一个调用的Java(非本地)方法建立一个新的栈帧。栈帧包括:为方法的局部变量所预留的空间,该方法的操作数栈,以及特定虚拟机实现需要的其他所有的信息.局部变量和操作数栈大小在编译时计算出来,并设置到class文件中去,然后虚拟机就能够了解到方法的栈帧需要多少内存.当虚拟机调用一个方法的时候,它为该方法创建恰当大小的栈帆,再将新的栈帧压入Java栈。


For an instance method, the virtual machine pops the objectref and args from the operand stack of the calling method's stack frame. It places the objectref on the new stack frame as local variable 0, and all the args as local variable 1, 2, and so on. The objectref is the implicit this pointer that is passed to any instance method.

处理实例方法时,虚拟机从所调用方法栈帧内的操作数栈中弹出objectref和args。虚拟机把objectref作为局部变量0放到新的栈帧中,把所有的args作为局部变量1,2,.........等处理。objectref是隐式传给所有实例方法的this指针。

?

For a class method, the virtual machine just pops the args from the operand stack of the calling method's frame and places them onto the new stack frame as local variable 0, 1, 2, and so on.

对于类方法,虚拟机只从所调用方法栈帧中的操作数栈中弹出参数,并将它们放到新的栈帧中去作为局部变量0,1,2..........

?

Once the objectref and args (or just the args, for a class method) have been placed into the local variables of the new frame, the virtual machine makes the new stack frame current and sets the program counter to point to the first instruction in the new method.

当objectref和args(对于类方法则只有args)被赋给新栈帧中的局部变量之后,虚拟机把新的栈帧作为当前栈帧,然后将程序计数器指向新方法的第一条指令。

?