Home Segments Top Top Previous Next

184: Mainline

In Chapter 6, you learned that Java is a call-by-value language: Memory is allocated for each parameter when a method is called, so parameter reassignments inside a method are prevented from propagating outside the method.

When an argument is a reference-type variable, the memory allocated for the parameter holds a copy of the address of the argument. Both addresses point to the same place:

Memory allocated for a              Memory that supplies a value 
reference variable in               for a corresponding reference 
calling method                      parameter in called method 
  |                                   | 
  v                                   v 
*----------*     Copy operation     *----------* 
| Address  | ---------------------> | Address  | 
*----------*                        *----------* 
  |                                   | 
  |       *---------------------------* 
  v       v 
*----------* 
| Instance | 
|          | 
*----------* 

Because the address is copied, a parameter reassignment inside a method can propagate outside the method.