Explaining how Java constructs are executed or evaluated
3. Executing method calls
We present the algorithm for executing a method call ---a call of a procedure, function, or constructor. It is crucial that you memorize the algorithm and be able to execute it yourself. It will help you understand how method calls work, when space is allocated for parameters and local variables, and why recursive method calls work. Further, this information will help you when debugging programs.
1. The frame for the method call
We start by explaining in a 3-minute video what a frame for a method call looks like. It contains all information needed to execute the call, including the parameters and local variables. After learning what a frame for a call is, you can already answer a few interesting questions! Read it here: AFrameForTheCall.pdf
2. Two data structures: the queue and the stack
We will need to use a data structure called the stack. In this 1.5-minute video, we introduce you to the stack and a similar data structure called the queue. Both the stack and the queue appear are used in many places in the real world. Read it here: BQueueAndStack.pdf
3. The call stack and the algorithm for executing a method call
In this 2-minute video, we introduce the call stack, a runtime data structure that contains a frame for each call of a method that is being executed and has not yet completed. We give the 4-step algorithm for executing a method call. Read it here: 03CMethodCall.pdf
4. How recursive calls work
We use the algorithm for executing a method call to explain why recursion works. The major idea is that each recursive call has its own frame-for-the-call on the call stack, to contain its parameters, local variables, and other information. A 2.5-minute video.
We don't execute a call to completion but leave you to do that. Read it here: DRecursion.pdf
As we said in the beginning, it is important that you memorize the algorithm for executing a method call and be ableto execute that algorithm yourself, by hand. We suggest that you execute a call like fact(3) (see DRecursion.pdf) carefully and slowly, drawing all the frames for the calls and following the algorithm carefully. Doing this will help you learn what it means to execute a method call.