Java Reference
In-Depth Information
Unlike some other languages, Java does
not
allow you to choose pass-by-value or pass-
by-reference—
all arguments are passed by value
. A method call can pass two types of values
to a method—copies of primitive values (e.g., values of type
int
and
double
) and copies
of references to objects. Objects themselves cannot be passed to methods. When a method
modifies a primitive-type parameter, changes to the parameter have no effect on the orig-
inal argument value in the calling method. For example, when line 30 in
main
of Fig. 7.13
passes
array[3]
to method
modifyElement
, the statement in line 45 that doubles the
value of parameter
element
has
no
effect on the value of
array[3]
in
main
. This is also
true for reference-type parameters. If you modify a reference-type parameter so that it
refers to another object, only the parameter refers to the new object—the reference stored
in the caller's variable still refers to the original object.
Although an object's reference is passed by value, a method can still interact with the
referenced object by calling its
public
methods using the copy of the object's reference.
Since the reference stored in the parameter is a copy of the reference that was passed as an
argument, the parameter in the called method and the argument in the calling method
refer to the
same
object in memory. For example, in Fig. 7.13, both parameter
array2
in
method
modifyArray
and variable
array
in
main
refer to the
same
array object in memory.
Any changes made using the parameter
array2
are carried out on the object that
array
references in the calling method. In Fig. 7.13, the changes made in
modifyArray
using
array2
affect the contents of the array object referenced by
array
in
main
. Thus, with a
reference to an object, the called method
can
manipulate the caller's object directly.
Performance Tip 7.1
Passing references to arrays, instead of the array objects themselves, makes sense for perfor-
mance reasons. Because everything in Java is passed by value, if array objects were passed,
a copy of each element would be passed. For large arrays, this would waste time and con-
sume considerable storage for the copies of the elements.
Store Grades
We now present the first part of our case study on developing a
GradeBook
class that in-
structors can use to maintain students' grades on an exam and display a grade report that
includes the grades, class average, lowest grade, highest grade and a grade distribution bar
chart. The version of class
GradeBook
presented in this section stores the grades for one
exam in a one-dimensional array. In Section 7.12, we present a version of class
GradeBook
that uses a two-dimensional array to store students' grades for
several
exams.
Storing Student Grades in an Array in Class
GradeBook
Class
GradeBook
(Fig. 7.14) uses an array of
int
s to store several students' grades on a sin-
gle exam. Array
grades
is declared as an instance variable (line 7), so each
GradeBook
ob-
ject maintains its
own
set of grades. The constructor (lines 10-14) has two parameters—
the name of the course and an array of grades. When an application (e.g., class
GradeBook-
Test
in Fig. 7.15) creates a
GradeBook
object, the application passes an existing
int
array
to the constructor, which assigns the array's reference to instance variable
grades
(line 13).
The
grades
array's
size
is determined by the
length
instance variable of the constructor's