System.arrayCopy() copies object or reference to object?(System.arrayCopy() 复制对象或对对象的引用?)
问题描述
I am having a final class NameAndValue
. I copied an array of NameAndValue
objects using System.arrayCopy()
and when I changed a NameAndValue
object in copied array, it gets reflected in the original array.
public final class NameAndValue {
public String name;
public String value;
public NameAndValue() { }
public NameAndValue(String name, String value) {
this.name = name;
this.value = value;
}
}
public class Main {
public static void main(String[] args) {
NameAndValue[] nv = new NameAndValue[4];
nv[0] = new NameAndValue("A", "1");
nv[1] = new NameAndValue("B", "2");
nv[2] = new NameAndValue("C", "3");
nv[3] = new NameAndValue("D", "4");
NameAndValue[] nv2 = new NameAndValue[4];
System.arraycopy(nv, 0, nv2, 0, 2);
nv2[2] = new NameAndValue("Y", "25");
nv2[3] = new NameAndValue("Z", "26");
for (int i = 0; i < 2; i++) {
NameAndValue[] nv3 = new NameAndValue[4];
System.arraycopy(nv2, 0, nv3, 0, 4);
nv3[2].value = String.valueOf(i);
nv3[3].value = String.valueOf(i + 1);
System.out.println(nv2[2].value);
System.out.println(nv2[3].value);
System.out.println("-----------------------");
}
}
}
I am getting output as:
0
1
-----------------------
1
2
-----------------------
I should have got output as 25 and 26 in all the cases, right?? Why does it get changed??
System.arrayCopy() copies object or reference to object?
Reference, it's a shallow copy. Surprisingly, the docs don't say that explicitly, just implicitly as they only talk about copying array elements, not recursively copying the things they reference.
It's exactly the same as if you had this:
NameAndValue nv1 = new NameAndValue("A", "1");
NameAndValue nv2 = nv1;
nv2.value = "4";
System.out.println(nv1.value); // 4
Each array element is like the nv1
and nv2
vars above. Just as nv1
and nv2
reference (point to) the same underlying object, so do the array entries, including when those entries are copied from one array to another.
这篇关于System.arrayCopy() 复制对象或对对象的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:System.arrayCopy() 复制对象或对对象的引用?


- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01