Java Reference
In-Depth Information
You can call the above swap() method using the following snippet of code:
int u = 75;
int v = 53;
swap(u, v);
/*At this point, u and v will be still 75 and 53, respectively */
By this time, you should be able to figure out why the values of u and v were not swapped when they were passed
to the swap() method. When the swap() method was called, the values of u and v were copied to the locations of the
x and y formal parameters, respectively. Inside the swap() method, the values of the formal parameters x and y were
swapped and the values of actual parameters u and v were not touched at all. When the method call was over, the
formal parameters x and y were discarded.
The advantages of using pass by value are as follows:
It is easy to implement.
If the data being copied is a simple value, it is faster.
The actual parameters are protected from any side effects when they are passed to the
method.
The disadvantages of using pass by value are as follows:
If the actual parameter is a complex data, such as a large object, it may be difficult, if not
impossible, to copy the data to another memory location.
Copying a large amount of data takes memory space and time, which may slow down the
method call.
Pass By Constant Value
Pass by constant value is essentially the same mechanism as pass by value with one difference that the formal
parameters are treated as constants, and hence, they cannot be changed inside the method's body. The values of
actual parameters are copied to the formal parameters as is done in pass by value. You can only read the value of
formal parameters inside the method's body if they are passed by constant value.
Pass By Reference
It is important that you do not confuse the phrases “reference” and “pass by reference.” A “reference” is a piece of
information (typically a memory address) that is used to get to the actual data stored at some other location. “Pass by
reference” is a mechanism to pass information from a caller's environment to a method using formal parameters.
In pass by reference, the memory address of the actual parameter is passed and the formal parameter is mapped
(or associated) with the memory address of the actual parameter. This technique is also known as aliasing, where
multiple variables are associated with the same memory location. The formal parameter name is an alias for the
actual parameter name. When a person has two names, no matter which of the two names you use, you refer to
the same person. Similarly, when a parameter is passed by reference, no matter which name you use in the code
(the actual parameter name or the formal parameter name), you are referring to the same memory location and
hence the same data.
In pass by reference, if the formal parameter is modified inside the method, the actual parameter sees the
modification instantaneously. Figure 6-7 depicts the memory state for actual and formal parameters when a
parameter to a method is passed by reference.
 
Search WWH ::




Custom Search