Skip to content
Advertisement

What does push into stack means? move or duplicate method?

When an object calls a function, the function would be pushed from the method area into the stack. My question is:

Q: What does that “push” mean in this context?

Does that simply mean move(from the method area) or copy of the function(a copy of the method pushed onto the stack)?

Advertisement

Answer

The method itself is not pushed onto the stack. The return address is pushed, as well as any parameters that are passed to the method that you’re calling. For example:

void foo() {
    int x = bar();
    int y = x*3;
    ...
}

var bar() {
    ...
    return 5;
}

When foo calls bar, the address of the next instruction to be executed (the assignment of y) is pushed onto the call stack. Control then branches to bar, which does its thing, and puts the value 5 in the return register (how values are returned isn’t really relevant here). Then, the runtime pops the return value from the call stack and branches back to that instruction. Execution continues with y = x*3.

If you do a search for [java method call stack], you’ll find some good examples with much more detailed explanations.

Advertisement