Java Reference
In-Depth Information
Actual Parameter
Name
Actual Parameter
Address
Actual Parameter
Value
Actual parameter value is
copied to formal parameter
when method is called.
Formal parameter value is
copied back to actual
parameter when method
returns.
Formal
Parameter
Address
Formal Parameter
Name
Actual Parameter
Value
Figure 6-10. Memory states for actual and formal parameters when the pass by result parameter passing mechanism
is used
It achieves the effect of pass by reference in a different way. In pass by reference, any modification made to the
formal parameter is visible to the actual parameter immediately. In a pass by value result, any modification to the
formal parameter is visible to the actual parameter only when the method call returns. If the formal parameter, which
uses a pass by value result, is modified multiple times inside a method, only the final modified value will be seen by
the actual parameter.
A pass by value result is used to simulate a pass by reference in distributed applications. Suppose you make a
remote method call, which executes on a different machine. The reference (memory address) of the actual parameter,
which exists in one machine, will not make any sense in the machine on which the remote method is executed. In
such cases, the client application sends a copy of the actual parameter to the remote machine. The value copied to the
formal parameter is on the remote machine. The formal parameter operates on the copy. When the remote method
call returns, the value of the formal parameter on the remote machine is copied back to the actual parameter on the
client machine. This gives the client code the functionality of passing parameters by reference to remote methods that
run on another machine.
Pass By Name
Typically, the actual parameter expression is evaluated before its value/reference is passed to a method. In pass by
name, the actual parameter's expressions are not evaluated when a method is called. The formal parameter's name
inside the body of the method is substituted textually with the expressions of their corresponding actual parameters.
Actual parameters are evaluated each time they are encountered during the execution of the method and they are
evaluated in the caller's context, not the method's context. If there is a name conflict between the local variables in
the method and the actual parameter expression during substitution, the local variables are renamed to give every
variable a unique name.
Pass by name is implemented using thunks. A thunk is a piece of code that computes and returns the value of
an expression in a specific context. A thunk is generated for each actual parameter and its reference is passed to the
method. At each use of a formal parameter, a call to thunk is made, which evaluates the actual parameter in the
caller context.
The advantage of pass by name is that the actual parameters are never evaluated unless they are used in the
method. This is also known as lazy evaluation. Contrast it with the pass by value mechanism, where actual parameters
are always evaluated before they are copied to the formal parameter. This is called eager evaluation. The disadvantage
of pass by name is that the actual parameters are evaluated every time the corresponding formal parameters are used
inside the method's body. It is also harder to follow the logic of a method if it uses the pass by name formal parameter,
which can also have side effects.
 
 
Search WWH ::




Custom Search