Primitive data type operations deal only with value. For
example, the following code shows that assigning primitive data
variable i
to another named j
simply passes a copy of the value in i
into j.
That is, a data value passes and not a reference or pointer to a
data location.
int
i = 1;
int j = i; // Now
j holds "1"
// that is, i's value passes
to j
i = 2; //
j still holds "1" |
Similarly, in method arguments, primitive variables
pass by value. That is, a copy is made and passed to the method.
Changes to the passed value do not affect the variable in the
calling code.
The following shows code on top that creates an instance of AClass
and invokes the aMethod
with an int
argument. The code on the bottom shows the AClass
definition and the aMethod.
...
int
i = 2;
AClass a1 = new
AClass ();
a1.aMethod
(i);
int
m = i; // m = i = 2 not 5
...
|
class
AClass
{
int j = 1;
void
aMethod (int k)
{
int
i = 10 * k;
k = 5;
j = k * 10;
}
}
|
The value in variable i
in the code on the top will be passed to variable k
in the aMethod
argument list shown in the AClass code
in the bottom box. When that method assigns a value of 5
to k,
it has no affect whatsoever on variable i
the code on the top.
Modify Reference Variables
You can always assign a new object to a reference variable. For
example, in the following code, we create two instances of the AClass,
"a"
and "b",
and then assign new values to the "j"
variable for each object:
...
AClass
a,b;
a = new AClass
();
b = new AClass
();
a.j = 4;
b.j = 40;
a = b; //
a now references same
//
object as does the b variable.
a.j = 400;
int x = b.j; // x = 400
since a and b now
//
reference same
object.
...
|
public
class AClass
{
public int j = 1;
void
aMethod (int k)
{
int
i = 10*k;
k = 5;
j = k * 10;
}
}
|
Then we assign the "b"
object to the "a"
reference with the a=b;
statement. So both the "a"
and "b"
variables now reference the same object.
Note that assigning a new object to the "a"
variable orphans the original object that "a"
pointed to previously. If an object is no longer referenced by any
other variables, then the garbage
collector will eventually reclaim the memory that it occupies.
References in Method Arguments
The argument list of a method can include object references. The
methods and data of that object can be accessed and modified by
the method.
However, the reference itself passes by value. That is, the JVM
makes a copy of the internal memory pointer and the copy of the
pointer goes into the argument value.
So if the reference variable in the method is set to a new object,
that will have no affect on the reference variable in the calling
method.
For example, in the following code, a reference to a BClass
object passes to the method of a AClass
object. In that method, the argument variable is assigned to a new
instance of the BClass.
However, when the process returns to the calling code, the originial
reference remains unaffected.
...
int
i = 2;
AClass a;
BClass b;
a = new AClass ();
b = new BClass ();
b.j
= 5;
a.aMethod
(b);
j
= b.j;// j = 20 not 5 or 100
...
|
class
AClass {
void
aMethod (BClass bb) {
bb.j = 20;
bb= new
BClass ();
bb.j = 100;
}
} |
class
BClass {
int j = 0;
} |
Since reference variables only hold pointers, the
test
if (a == b)...
simply tests whether a
and b point to the
same object, not if the referenced objects have equal data values.
If you need to test whether two objects hold the same data values,
many classes provide an "equals()"
method such that
a.equals(b)
returns true if the object b
data matchs that in object a.
Latest update: Nov.8, 2006
|