Java Reference
In-Depth Information
Pass by Result
You can think of pass by result as the opposite of pass by value. In pass by value, the value of the actual parameter
is copied to the formal parameter. In pass by result, the value of the actual parameter is not copied to the formal
parameter. The formal parameter is considered an uninitialized local variable when the method execution starts.
During the method execution, the formal parameter is assigned a value. At the end of the method execution, the value
of the formal parameter is copied back to the actual parameter.
Figure 6-9 depicts the memory state for the actual and formal parameters when the pass by result mechanism of
parameter passing is used.
Actual Parameter
Name
Actual Parameter
Address
Actual Parameter
Value
Formal parameter
value is copied to
actual parameter
Formal
Parameter
Address
Formal
Parameter
Value
Formal Parameter
Name
Figure 6-9. Memory states for actual and formal parameters when the pass by result parameter passing mechanism
is used
Sometimes the formal parameters are also known as OUT parameters when the pass by result mechanism is used.
They are called OUT parameters because they are used to copy out a value from the method to the caller's environment.
Likewise, formal parameters are sometimes known as IN parameters if they uses the pass by value mechanism
because they are used to copy in the value of the actual parameter.
Pass by Value Result
Also known as pass by copy-restore, this is a combination of pass by value and pass by result (hence the name “pass
by value result”). It is also known as the IN-OUT way of passing parameters. When a method is called, the value of
the actual parameter is copied to the formal parameter. During the execution of the method, the formal parameter
operates on its own local copy of data. When the method call is over, the value of the formal parameter is copied back
to the actual parameter. This is the reason that it is also called pass by copy-restore. It copies the value of the actual
parameter in the beginning of the method call and restores the value of formal parameter in the actual parameter at
the end of the method call. Figure 6-10 depicts the memory state of actual and formal parameters when the pass by
value result mechanism is used to pass parameters.
 
 
Search WWH ::




Custom Search