5.3 Mutable and Immutable Objects | 243
When the method returns, whatever value is in the pa-
rameter is discarded. It is not copied back into a variable in
the argument. Thus, values of primitive types may be passed
into methods, but changes to those values aren’t returned.
Figure 5.6 illustrates the passing of primitive types. Keep in
mind that we are referring here only to the parameters, not
to the return value of a value-returning method.
With reference types such as Stringand other classes,
a variable contains the address where an object’s fields are
stored in memory. This address is the value copied from
the argument to the parameter. Only one copy of the ob-
ject’s fields exists, which both the calling code and the
method use. Figure 5.7 illustrates the difference between
passing primitive and reference types to a method.
Changes to the primitive type parameters don’t affect
the argument. But in the situation pictured in Figure 5.7,
wouldn’t the changes to a reference type parameter, such
as String, also change the argument? For many reference
types the answer would be yes, but Stringis an immutable
class.
Any object that stores information is said to havestate. The object’s state
is the current set of values that it contains. An object is instantiated with an ini-
tial state. If any of its methods can subsequently change its state, then the ob-
ject is said to bemutable.System.out, for example, is a mutable object: Itsprint
method changes its state—it makes the appearance of the window change by
adding text to it. If an object doesn’t have any methods that can change its
state, it is immutable. AStringis an immutable object; we can merely retrieve
its contents.
Thus there is no way to change a Stringobject; we can only
assign a new value to a Stringvariable. Because each string value
occupies a different place in memory, the effect of assigning a
new value to a Stringvariable is to store a different address in it.
A parameter of type Stringis initially given a copy of the address
of the string argument. When we assign a new value to it, the ad-
dress is replaced by the address of the new value. The argument
itself is left unchanged. Figure 5.8 illustrates what happens when
we change the value of a parameter of type String.
In contrast to the primitive types and immutable classes like
String, some classes provide methods for directly changing in-
stance fields. For example, we could write a method that takes
System.outas a parameter and uses System.out.printto display
the string "Java". The method’s parameter receives the address
where System.outis stored. Calling printwith the parameter di-
rectly changes the same window that the argument refers to.
Argument Parameter
Call
double arg1
double arg1
double arg1
3 21. 89
3 21. 89
3 21. 89
During execution
Return
doubleparam1
double param 1
doubleparam1
3 21. 89
91773 .11 2
paraml = 91773.112;pparaml =paraml =araml = ;;;
Figure 5.6 Passing a Primitive Type as an Argument to a
Parameter
Immutable object An object
whose state cannot be changed
once it is created
State The information stored
in an object at any given time
Mutable object An object
whose state can be changed
after it is created
Argument Parameter
PrimitiveType
double arg1
String arg2
3 21. 89
Addr 317401
Memory Address
317401
Addr 317401
ReferenceType
doubleparam1
String param 2
3 21. 89
"Java"
Figure 5.7 Passing Primitive and Reference Types