Java Reference
In-Depth Information
an array object. If two variables contain references to the same object, the state of the ob-
ject can be modified using one variable's reference to the object, and then the altered state
can be observed through the reference in the other variable.
Example 4.3.1-2. Primitive and Reference Identity
class Value { int val; }
class Test {
public static void main(String[] args) {
int i1 = 3;
int i2 = i1;
i2 = 4;
System.out.print("i1==" + i1);
System.out.println(" but i2==" + i2);
Value v1 = new Value();
v1.val = 5;
Value v2 = v1;
v2.val = 6;
System.out.print("v1.val==" + v1.val);
System.out.println(" and v2.val==" + v2.val);
}
}
This program produces the output:
i1==3 but i2==4
v1.val==6 and v2.val==6
object created by the only
new
expression, while
i1
and
i2
are different variables.
to state by multiple threads (§17).
4.3.2. The Class
Object
as follows:
• The method
clone
is used to make a duplicate of an object.