Java Reference
In-Depth Information
Actual Parameter
Name
Actual Parameter
Address
Actual Parameter
Value
Formal Parameter
Name
Formal parameter is associated
with the address of actual
parameter in the beginning of the
method call
Figure 6-7. Memory states of actual and formal parameters when the parameters are passed by reference
Many topics use the phrase “pass by reference.” However, they do not mean the one we are discussing in this
section. They really mean “pass by reference value,” which I will discuss in the next section. Note that in pass by
reference, you do not allocate separate memory for the formal parameter. Rather, you just associate the formal
parameter name to the same memory location of the actual parameter.
Let's do your increment() method call exercise again. This time, you assume that the num parameter is passed by
reference.
// Assume that num is passed by reference
void increment(int num) {
/* #2 */
num = num + 2;
/* #3 */
}
You will call the increment() method with the following snippet of code:
int id = 57;
/* #1 */
increment(id);
/* #4 */
Table 6-5 describes the memory states for the actual parameter and formal parameter, before, after, and during
the increment() method's invocation. Note that at #4, the formal parameter num no longer exists in memory and still
the actual parameter id has the value of 59 after the method call is over.
 
Search WWH ::




Custom Search