[英]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: 我的理解是:
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: 对于解释器,假设一个实例方法,并采取一些小的自由:
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.