Pass by Value Vs Pass by Reference - Part II
In figure 1 the memory assignments for the object and the object
variable are illustrated. This corresponds to the state of the sb and
sbTest variables just after line 8 in the code snippet. In the code
snippet, the sbMethod() method is called with sb as an argument. Within
the method, a copy of sb exists as a local variable named sbTest. The
memory assignments just after the sbMethod() begins execution are shown
in Figure 1b. Note that sb and sbTest refer to the same object.
Figure1. Memory assignments for call-by-reference example (StringBuffer objects)
In the sbMethod, the object is modified using the insert() method of the StringBuffer class. The method is called through the instance variable sbTest. The memory assignments just after insert() executes are shown in Figure 1c. After the sbMethod() finishes execution, control passes back to the main() method and sbTest ceases to exist. The memory assignments just after line sbMethod(sb) in the code snippet are shown in Figure 1d. You can that the original object has changed, and that the original object variable, sb, now refers to the updated object. This is a typical example of “pass by reference”. It demonstrates that methods can change the objects instantiated in other methods when they pass a reference to the object as an argument.
The String class is an exception in pass by reference. As Strings are immutable objects in java i.e. once instantiated the string objects cannot be changed. To understand this concept lets see figure 2.
Figure2: Memory assignments for call-by-reference example (String objects)
In the code snippet, the sMethod is passing “s” as an argument. In the method’s definition a copy of “s” exist as a local variable called “sTest”. The memory assignments just after the sMethod() begins execution are shown in Figure 2b. It should be noted that s & sTest refers to the same object. At this point, there is no difference in how memory was assigned for the String or StringBuffer objects. The substring() method of the String class is called to extract the string "fun" from the original string. The result is the instantiation of a new String object. A reference to the new object is assigned to sTest. The memory assignments just after the line sTest = sTest.substring(8, 11) is shown in Figure2c.
After the sMethod() finishes execution, control passes back to the main() method and sTest ceases to exist. The memory assignments just after the statement sMethod(s) in the code snippet are shown in Figure 2d. The important point to note here is that the original object did not change. The String object containing "fun" is now redundant and its space is eventually reclaimed.
For integer variables the memory assignments are shown in figure 3. When an integer variable is passed as an argument to a method, it is passed by value. In the test program, an integer variable named i is declared and initialized
with value 25 (see figure 3a).
Figure3. Memory assignments for call-by-value example (int variables)
In the main() method the iMethod() is called with i as an argument. Within the method, a copy of i exists as a local variable named iTest. The memory assignments just after iMethod() begins execution are shown in Figure 3b. Note that i and iTest are distinct: They are two separate variables that happen to hold the same value.
In the iMethod() method, the variable iTest is re-assigned the value 9. The memory assignment is shown in Figure 3c. After the iMethod() finishes execution, control passes back to the main() method and iTest ceases to exist. The memory assignment is shown in Figure 3d. The important point to notice here is that the value of the original variable did not change.
|All rights reserved. Terms and Conditions|