C graphics
C graphics

Reputation: 7458

If arrays are object in java why the assignment between arrays is deep copy

If arrays are object as stated in Is an array an object in java then why the output of the code snipped below is [1,1,1]?

I thought after the execution of statement "a=b;" a and b are still pointing to the same content! Isn't it supposed to be shadow copy between objects?

import java.util.Arrays;

public class Local {
    int [] a = null;
    public Local(){
    int [] b = {1,1,1};
    int [] c = {5,5};
    a=b;
    b=c;// 'a' should change too as a and b are both objects! right?
    }

    public static void main(String[] args) {
        Local local = new Local();
        System.out.println(Arrays.toString(local.a));
    }

}

Upvotes: 0

Views: 119

Answers (4)

wangyuxiang
wangyuxiang

Reputation: 1

At first, b is pointed to the array object {1,1,1} and you assign b to a, so a is pointed to {1,1,1} so the out is {1,1,1}

Upvotes: 0

lxcky
lxcky

Reputation: 1668

Let me try to explain it line per line:

int [] b = {1,1,1};

enter image description here

On this line, three things happened.

  1. Created a variable named b.
  2. Created an array of int {1,1,1}
  3. Assigned b to the array of intcreated on step 2.

int [] c = {5,5};

enter image description here

Same thing happened here

  1. Created a variable named c.
  2. Created an array of int {5,5}
  3. Assigned c to the array of int created on step 2.

a=b;

Now we also assigned a to whatever the value of b is, which in this case is the array of int {1,1,1}

Now we have something like

enter image description here

b=c; // 'a' should change too as a and b are both objects! right?

What happened here is that we assigned b to whatever c's value is (int array {5,5}), now b is not pointing to {1,1,1} anymore and since Java is pass by value, a's value remained. It's not like a is pointing to the reference of b that whatever b is pointing to, a will point to it too.

enter image description here

Hope this helps.

Upvotes: 3

Manjunath
Manjunath

Reputation: 1685

// 'a' should change too as a and b are both objects! right?

No both a and b are just reference variables pointing to the same array object {1,1,1}.

With the below line you are making b to refer to altogether different array object where as a would still be pointing to the same array object {1,1,1} as the reference of only b but not a is changed by executing the below line

b = new int[] {2, 2};

Also by making a = b you are making them point to one single array object {1,1,1} and there is no deep/shallow copy happening here.

Upvotes: 3

Ker p pag
Ker p pag

Reputation: 1588

the variable a would not automatically update itself. mainly because

b = c //is like b = new int[]{5,5};

it is the same concept in your question earlier with

b = new int[]{2,2,2};

a is pointing to b's int array which is [1,1,1]

and you are telling b to point to c which is [5,5]

a => b's array
b => c's array

so a will retain its object and b will have a new one.

Upvotes: 1

Related Questions