简体   繁体   中英

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).

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

2) The current method pauses

3) The arguments of the newly called method are pushed to the stack

4) The method code runs

5) After the method finished running, we pop the stack. 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

7) Code continues with the calling method. All it's stack variables remain intact.

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

ADDENDUM:

@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".

  • 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

  • Finally, Marko Topolnik is correct. "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.

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.
  2. The method pointer is located in the Class object. (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. 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.
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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