This is part of the answer about stack and heap in Java:
So, why have the stack or the heap at all? For things that leave scope, the stack can be expensive. Consider the code:
void foo(String arg) { bar(arg); ... } void bar(String arg) { qux(arg); ... } void qux(String arg) { ... }
The parameters are part of the stack too. In the situation that you don't have a heap, you would be passing the full set of values on the stack. This is fine for "foo" and small strings... but what would happen if someone put a huge XML file in that string. Each call would copy the entire huge string onto the stack - and that would be quite wasteful.
This is what I read in The Java ® Virtual Machine Specification Java SE 8 Edition (Chapter 2.5.2. Java Virtual Machine Stacks):
It [Java Virtual Machine stack] holds local variables and partial results, and plays a part in method invocation and return.
What I know is that when I pass a reference as a parameter to a method, a local variable is created inside the method. The local variable holds the same reference as the passed variable. For example, something like this
void foo(Bar bar) {
// Do something with bar
}
turns into something like this:
void foo(Bar bar) {
Bar localBar = bar;
// Do something with localBar
}
So my question is:
Does Java copy method parameters to the stack frame of the called method? From the answer I refer to at the start of page, I understand they are copied, and it's a deep copy. Almost sure, I am wrong.