0

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));
    }

}
Community
  • 1
  • 1
C graphics
  • 7,308
  • 19
  • 83
  • 134
  • 2
    it is because you create a `new` int that makes it another object. a would retain its value and b will have a `new int` – Ker p pag Aug 30 '14 at 05:18
  • @Kerppag What about now that I changed "b=new" to "b=c"? – C graphics Aug 30 '14 at 05:23
  • it is the same concept. since `c` is stored differently in memory a will retain the b's content. and thus c is another new int. as you notice if you use b[0] = 2; the output of a would be [2,1,1]; since a and b are pointing in the same array – Ker p pag Aug 30 '14 at 05:27
  • @Cgraphics even when you change to b=c you are actually making b to refer to whatever array object c refers to but with this you are not telling a to refer to whatever b is referring. Reference is to objects – Manjunath Aug 30 '14 at 05:28

4 Answers4

3
// '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.

Manjunath
  • 1,685
  • 9
  • 9
3

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.

lxcky
  • 1,668
  • 2
  • 13
  • 26
1

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.

Ker p pag
  • 1,568
  • 12
  • 25
0

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}