简体   繁体   English

在Java中调用方法后会发生什么

[英]What happens after a method is called in Java

This looks like a silly question but I found it is hard to get it right. 这看起来像一个愚蠢的问题,但我发现它很难做到正确。 I have asked different people but couldn't get an ideal answer. 我问过不同的人,但无法得到理想的答案。

I want to know what happens after we call a normal method in Java (Provided in a single threaded environment). 我想知道在Java中调用普通方法后会发生什么(在单线程环境中提供)。

My understanding is that: 我的理解是:

  1. All current stack variables are poped-up and stored somewhere (where?) 所有当前的堆栈变量都被加载并存储在某处(其中?)
  2. The current method call halts 当前方法调用暂停
  3. The arguments of the newly called method are pushed to the stack 新调用方法的参数被推送到堆栈
  4. The method code runs 方法代码运行
  5. After the method finished running, the stack is again emptied and the old stack contents is again restored. 方法完成运行后,再次清空堆栈并再次恢复旧堆栈内容。 (What happened if the function returns a value?). (如果函数返回值,会发生什么?)。
  6. Code continues with the calling method. 代码继续使用调用方法。

This is a very incomplete and possibly wrong answer. 这是一个非常不完整的,可能是错误的答案。 Can someone provide a more detailed description? 有人可以提供更详细的描述吗?

Many thanks. 非常感谢。

No, that's actually fairly accurate: 不,这实际上相当准确:

1) current stack variables remain on the stack 1)当前堆栈变量保留在堆栈中

2) The current method pauses 2)当前方法暂停

3) The arguments of the newly called method are pushed to the stack 3)新调用方法的参数被推送到堆栈

4) The method code runs 4)方法代码运行

5) After the method finished running, we pop the stack. 5)方法运行完毕后,我们弹出堆栈。 The called method's stack variables are no longer valid - they no longer "exist" at this point. 被调用方法的堆栈变量不再有效 - 此时它们不再“存在”。

6) We pass the return value (if any) to the caller 6)我们将返回值(如果有的话)传递给调用者

7) Code continues with the calling method. 7)代码继续调用方法。 All it's stack variables remain intact. 它的所有堆栈变量都保持不变。

============================== ==============================

ADDENDUM: 附录:

@Kevin - @Kevin -

  • Conceptually, I think you got it just about right. 从概念上讲,我认为你做得恰到好处。 I clarified a few points, I hope that helps. 我澄清了几点,希望有所帮助。

  • David Wallace's link is very good if you want to go in depth on how the JVM implements "method calling". 如果你想深入了解JVM如何实现“方法调用”,David Wallace的链接非常好。

  • Here is a good overview on how "a stack" works. 这里有一个关于“堆栈”如何工作的很好的概述。 Any stack, calling any subroutine - not just Java: http://en.wikipedia.org/wiki/Call_stack 任何堆栈,调用任何子程序 - 不只是Java: http//en.wikipedia.org/wiki/Call_stack

  • Finally, Marko Topolnik is correct. 最后,Marko Topolnik是正确的。 "The reality" is almost always complex enough that it doesn't lend itself to a simple, one-size-fits all answer. “现实”几乎总是很复杂,它不适合简单,一刀切的答案。 But I definitely think your understanding is good. 但我绝对认为你的理解是好的。 At least at the 10,000 foot level. 至少在10000英尺的高度。

IMHO... 恕我直言...

For the interpreter, assuming an instance method, and taking some minor liberties: 对于解释器,假设一个实例方法,并采取一些小的自由:

  1. The object pointer is used to reference the object, and from there the Class object. 对象指针用于引用对象,并从那里引用Class对象。
  2. The method pointer is located in the Class object. 方法指针位于Class对象中。 (The lookup to convert method name to method index was largely done when the class was loaded, so this is basically just an array index operation.) (将方法名称转换为方法索引的查找主要在加载类时完成,因此这基本上只是一个数组索引操作。)
  3. Generally some sort of a "mark" is pushed onto the JVM stack. 通常,某种“标记”被推送到JVM堆栈上。 This would contain the caller's instruction pointer, and a pointer to the base of his stack. 这将包含调用者的指令指针,以及指向其堆栈基础的指针。 (Lots of different implementations here.) (这里有很多不同的实现。)
  4. The method's definition is consulted to see how many local vars are needed. 查看方法的定义以查看需要多少本地变量。 That many blank elements are pushed onto the stack. 许多空白元素被压入堆栈。
  5. The object ("this") pointer is stored in local var 0, and any parms are stored in 1,2,3... as appropriate. 对象(“t​​his”)指针存储在本地var 0中,任何parms都存储在1,2,3 ......中。
  6. Control is transferred to the called method. 控制转移到被调用的方法。

On return, the stack is popped down to the point where the call started, any return value is pushed onto the stack, and control is transferred back to the caller. 返回时,堆栈会弹出到调用开始的位置,任何返回值都会被压入堆栈,控制权将转移回调用者。

Compiled code is conceptually similar, only it uses the "C" stack, and interpreted code in a JITC environment will make use of both the JVM stack and the "C" stack. 编译代码在概念上类似,只使用“C”堆栈,JITC环境中的解释代码将同时使用JVM堆栈和“C”堆栈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM