public class Test {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5};
int[] b = a;
int[] c = {6, 7, 8};
a = c;
for(int i = 0; i < a.length; i ++)
System.out.print(a[i] + " ");
System.out.print("\n");
for(int i = 0; i < b.length; i ++)
System.out.print(b[i] + " ");
System.out.print("\n");
}
}
I have initialized array a and assigning reference of a to new array b. Now I initialized a new array c and passed its reference to array a. Since array b is reference to array a, b should have new values that are in c but b is having old values of a. What is the reason behind it?
Output is given below -
Output -
6 7 8
1 2 3 4 5
Don't be irritated by the name 'list'. The images are taken from a python visualization, but the principle is the same in Java
Array a
is assigned with a new array:
![](https://www.manongdao.com/static/images/pcload.jpg)
Array b
is assigned to the instance behind a
:
![](https://www.manongdao.com/static/images/pcload.jpg)
Array c
is assigned with a new array:
![](https://www.manongdao.com/static/images/pcload.jpg)
And finally a
is assigned to the instance behind c
, b
was not re-assigned and still keeps a reference to the old a
:
![](https://www.manongdao.com/static/images/pcload.jpg)
Images taken from a visualization on pythontutor.com
When you assigned value of a to b, it means b is referring to same space allocated to array a. This means b will pick up any changes made to the array a, but if any changes made to the variable a. If a is made to refer to new array, b will still refer the old a reference.
b = a; // b basically referring to memory used by array a
a = c; // reference pointed by a has changed, but b is still referring the old array a
When you do b=a
b references to a. However when you to a=c
a is referring to c, but still b refers to the old object (address of this object as value been assigned and it is constant) a
that you assigned because that is what it contains when you assigned.
Unless you reassign it, it won't change.
Suppose you think of an object as a house, and a reference as a piece of paper with the address of a house written on it. a
, b
, and c
are all references. So when you say
int[] a = {1, 2, 3, 4, 5};
you're building a house with 1, 2, 3, 4, and 5 in it; and a
is a piece of paper with the address of that house.
int[] b = a;
b
is another reference, which means it's another piece of paper. But it has the address of the same house on it.
int[] c = {6, 7, 8};
a = c;
Now we build a new house and put 6, 7, and 8 into it. c
will be a piece of paper with the address of the new house. When we say a = c
, then the slip of paper that used to be a
is thrown out, and we make a new piece of paper with the address of the new house. That's the new value of a
.
But b
was a separate piece of paper. It hasn't changed. Nothing we've done has changed it.
References are like that.
This is why:
What is Object Reference Variable?
You have 2 objects (the arrays) and 3 references (a, b, c) to the objects. You're just swapping around where things are pointed.