How does this array variable reference act?

gym*_*y42 0 java

I was wondering how something like this would work.

int[] a = {5, 3, 4};

int[] b = {3, 4, 5};

a = b;
Run Code Online (Sandbox Code Playgroud)

Does this mean that a will now reference b. So if I do a[0] it will be 3?

Also if this is the case what happens to the items in the old array?

T.J*_*der 9

Does this mean that a will now reference b. So if I do a[0] it will be 3?

Sort of. a doesn't reference b (the variable), it references the same array that b refers to. There's no connection between a and b, it's just that they both refer to the same array (after the a = b assignment).

Also if this is the case what happens to the items in the old array?

The old array is eligible for garbage collection, since nothing refers to it anymore. Since it's an array of primitive values, the items are part of the array, so GC'ing the array inherently means the items are GC'd.

In memory, initially you had:

              +?????????+
a:Ref33423???>| (Array) |
              +?????????+
              | 5       |
              | 3       |
              | 4       |
              +?????????+

              +?????????+
b:Ref54687???>| (Array) |
              +?????????+
              | 3       |
              | 4       |
              | 5       |
              +?????????+

Then after the a = b;, you have:

              +?????????+
              | (Array) |
              +?????????+
              | 5       |
              | 3       |
              | 4       |
              +?????????+
a:Ref54687??+
            |
            |  +?????????+
            +?>| (Array) |
            |  +?????????+
            |  | 3       |
b:Ref54687??+  | 4       |
               | 5       |
               +?????????+

...and eventually GC will remove that orphaned array:

a:Ref54687??+
            |
            |  +?????????+
            +?>| (Array) |
            |  +?????????+
            |  | 3       |
b:Ref54687??+  | 4       |
               | 5       |
               +?????????+

我应该注意的是,如果它是一个对象数组,则该数组以及该数组所引用的每个对象的寿命可能都不同,GC'ing数组不一定意味着其中的对象是GC'(取决于是否还有其他参考文献)。