Array assignment and reference in Java

前端 未结 5 1485
误落风尘
误落风尘 2020-12-11 08:24
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;
         


        
5条回答
  •  没有蜡笔的小新
    2020-12-11 08:43

    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.

提交回复
热议问题