0

So, lets say I have 2 arrays defined like this:

int[] a = {1,2,3};
int[] b = a;

a[1] = 35; // b[1] becomes 35
b[0] = -5; // why would a[0] become -5?

So, I can reason that changing a value in a will automatically change the value in b (b is pointing to a). But, if I change value in b, why would it affect a? Wouldn't that be as if a is pointing to b (they are interchangeable)?

I was confused with this concept and would like some clarification. Any advice would be appreciated. Thanks.

  • 4
    In your code `a` and `b` are references to the **same** memory location. – PM 77-1 Feb 18 '16 at 04:44
  • This might help you : http://stackoverflow.com/a/1750197/3226981 – Suyash Feb 18 '16 at 04:47
  • `a` and `b` are both pointing to the same object which is something _else_ off in memory somewhere. `b` isn't pointing to `a`, it's pointing to the same object that `a` is pointing to. – Louis Wasserman Feb 18 '16 at 05:06

4 Answers4

1

When you create the variable int[] a = {1,2,3};, by using the brackets { } just as by saying new int[n] java allocates enough memory for those values and now a references that address in memory.

Then, when you create the new variable int[] b = a;, you aren't allocating memory to that variable b, rather b references the same address which a references.

Now, when you change something in a or b the change occurs at the address referenced by both those variables and will be reflected by both.

Moshe Stauber
  • 401
  • 2
  • 11
1

Let's do an analogy here. The objects (such as arrays) that you create are like balloons. And the variables (a and b) are like children. And the children can hold a single balloon with a string. And that string is what we call a reference. On the other hand, one balloon can be held by multiple children.

In your code, you created a single balloon i.e. an array {1, 2, 3}. And let a child called a hold it. Now you can tell child a to modify items in the array because the child a is holding it.

In the second line, you tell another child b to hold the balloon that a is holding, like this:

int[] b = a;

Now a and b are actually holding the same balloon!

When you do this:

a[1] = 35;

Not only is the balloon that a is holding been changed, but also is the balloon held by b been changed. Because, well, a and b are holding the same balloon.

The same thing happens when you do:

b[0] = -5;

That's how reference types work in Java. But please note that this doesn't apply to primitive types, such as int or float.

For example,

int a = 10;
int b = a;
b = 20; //a is not equal to 20!!!
Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

Because Java Array is considered Reference Object.

int[] b = a;    // variable 'b' points same memory location with 'a'

If you want to copy all value of a, write code below

@Test
public void arrayCopy1() {
    int[] a = {1, 2, 3};
    int[] b = a;

    System.out.println(a); //Point [I@64a294a6
    System.out.println(b); //Point [I@64a294a6

    printArrayValues(a);   //[1,2,3]
    printArrayValues(b);   //[1,2,3]
    a[0] = 10;
    printArrayValues(a);   //[10,2,3]
    printArrayValues(b);   //[10,2,3]
}

@Test
public void arrayCopy2() {
    int[] a = {1, 2, 3};
    int[] b = Arrays.copyOf(a, a.length);

    System.out.println(a); //Point [I@64a294a6
    System.out.println(b); //Point [I@7e0b37bc

    printArrayValues(a);   //[1,2,3]
    printArrayValues(b);   //[1,2,3]
    a[0] = 10;
    printArrayValues(a);   //[10,2,3]
    printArrayValues(b);   //[1,2,3]
}

private void printArrayValues(int[] arr) {
    System.out.print("[");
    for(int i = 0 ; i < arr.length ; i++) {
        System.out.print(arr[i]);
        if (i != arr.length-1)
            System.out.print(",");
    }
    System.out.println("]");
}
Yoonsung.Jung
  • 526
  • 4
  • 3
0

Because when you set int[] b = a you didn't simply copied the values of a and gave them to b you gave to b the memory location of a, therefore if you change one, you change the other

T4l0n
  • 595
  • 1
  • 8
  • 25