Java Reference
In-Depth Information
The argument last is copied into the parameter lastName . The argument first is
copied into the parameter firstName . What gets copied? Well, because last and first are
references, they contain memory addresses, and that is what gets copied. The result is that
lastName points to the same String object as last , which in this example is “Einstein” .
Similarly, firstName points to “Albert” , as shown in Figure 1.11. The objects did not get
copied! There is still only one String object with the value “Einstein” in memory and only
one String object with the value “Albert” in memory.
FIGURE 1.11
The arguments from main are copied into the parameters of findByName .
“Albert”
first
firstName
“Einstein”
last
lastName
The memory of the
call stack for main
Heap
The memory of the
call stack for findByName
Because String objects are immutable, the parameters lastName and firstName cannot
change the objects “Albert” or “Einstein” . Setting the parameters equals to “Jane” and “Doe”
has no effect on first and last , as Figure 1.12 shows. Therefore, the output of that code is
Albert Einstein
FIGURE 1.12 String objects are immutable, so findByName cannot change
first and last .
“Albert”
first
firstName
“Einstein”
last
lastName
“Jane”
“Doe”
The memory of the
call stack for main
Heap
The memory of the
call stack for findByName
Search WWH ::




Custom Search