Assigning Array to new variable, changing original changes the new one

前端 未结 4 1329
暖寄归人
暖寄归人 2020-12-11 22:22
int[] test = {0,1,2,3,4,5};
    int[] before = test;
    System.out.println(before[2]);
    test[2] = 65;
    System.out.println(before[2]);

The fi

4条回答
  •  佛祖请我去吃肉
    2020-12-11 23:08

    Why would you not expect this to happen. You are simply assigning two variables to the same array, and then You are assigning index #2 of that array as 65, so, you should get 65 the next time you print what's on index #2.

    see examples at http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

提交回复
热议问题