Summary: Deep Copy vs. Shallow Copy vs. Lazy Copy
Object copy
An object copy is an action in computing where a data object has its attributes copied to another object of the same data type. An object is a composite data type in object-oriented programming languages. The copying of data is one of the most common procedures that occurs in computer programs. An object may be copied to reuse all or part of its data in a new context.
Shallow copy vs. Deep copy vs. Lazy copy
https://www.cs.utexas.edu/~scottm/cs307/handouts/deepCopying.htm
When creating copies of arrays or objects one can make a deep copy or a shallow copy. This explanation uses arrays.
Recall array variables in Java are references or pointers. A shallow copy can be made by simply copying the reference.
public class Ex{
private int[] data;
public Ex(int[] values){
data = values;
}
public void showData(){
System.out.println( Arrays.toString(data) );
}
}
The above code shows shallow copying. data simply refers to the same array as vals.
This can lead to unpleasant side effects if the elements of values are changed via some other reference.
public class UsesEx{
public static void main(String[] args){
int[] vals = {-5, 12, 0};
Ex e = new Ex(vals);
e.showData(); // prints out [-5, 12, 0]
vals[0] = 13;
e.showData(); // prints out [13, 12, 0]
// Very confusiin, because I didn't intentionally change anything about the
// object e refers to.
}
}
A deep copy means actually creating a new array and copying over the values.
public class Ex{
private int[] data;
public Ex(int[] values){
data = new int[values.length];
for(int i = 0; i < data.length; i++)
data[i] = values[i];
}
public void showData(){
System.out.println( Arrays.toString(data) );
}
}
The above code shows deep copying.
Changes to the array values refers to will not result in changes to the array data refers to.
Shallow copy[edit]
One method of copying an object is the shallow copy. In the process of shallow copying A, B will copy all of A's field values.[1][2][3][4] If the field value is a memory address it copies the memory address, and if the field value is a primitive type it copies the value of the primitive type.
Deep copy
An alternative is a deep copy. Here the data is actually copied over. The result is different from the result a shallow copy gives. The advantage is that A and B do not depend on each other but at the cost of a slower and more expensive copy.
Lazy copy
A lazy copy is a combination of both strategies above. When initially copying an object, a (fast) shallow copy is used. A counter is also used to track how many objects share the data. When the program wants to modify an object, it can determine if the data is shared (by examining the counter) and can do a deep copy if necessary.
Summary: Deep Copy vs. Shallow Copy vs. Lazy Copy的更多相关文章
- [CareerCup] 13.4 Depp Copy and Shallow Copy 深拷贝和浅拷贝
13.4 What is the difference between deep copy and shallow copy? Explain how you would use each. 这道题问 ...
- Deep Learning and Shallow Learning
Deep Learning and Shallow Learning 由于 Deep Learning 现在如火如荼的势头,在各种领域逐渐占据 state-of-the-art 的地位,上个学期在一门 ...
- python deep copy and shallow copy
Python中对于对象的赋值都是引用,而不是拷贝对象(Assignment statements in Python do not copy objects, they create bindings ...
- deep copy and shallow copy
链接A:浅拷贝就是成员数据之间的一一赋值:把值赋给一一赋给要拷贝的值.但是可能会有这样的情况:对象还包含资源,这里的资源可以值堆资源,或者一个文件..当值拷贝的时候,两个对象就有用共同的资源,同时对资 ...
- python(41):copy拷贝(深拷贝deepcopy与浅拷贝copy)
Python中的对象之间赋值时是按引用传递的,如果需要拷贝对象,需要使用标准库中的copy模块. 1.copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象. 2.copy.deepco ...
- UVa 10176 - Ocean Deep ! - Make it shallow !!
题目大意:判断一个很大的二进制能否被131071整除.在二进制转十进制的过程中不断取模,最后判断结果是否是0就可以了. #include <cstdio> #include <cst ...
- python中的shallow copy 与 deep copy
今天在写代码的时候遇到一个奇葩的问题,问题描述如下: 代码中声明了一个list,将list作为参数传入了function1()中,在function1()中对list进行了del()即删除了一个元素. ...
- 由Python的浅拷贝(shallow copy)和深拷贝(deep copy)引发的思考
首先查看拷贝模块(copy)发现: >>> help(copy)Help on module copy:NAME copy - Generic (shallow and dee ...
- iOS CMSampleBuffer deep copy
extension CVPixelBuffer { func copy() -> CVPixelBuffer { precondition(CFGetTypeID(self) == CVPixe ...
随机推荐
- LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)
题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重 ...
- 【CF884F】Anti-Palindromize 费用流
[CF884F]Anti-Palindromize 题意:定义一个串是反回文的,当且仅当对于1<=i<=len,$a_i!=a_{len-i+1}$. 现在给出一个长度为n的串S(n是偶数 ...
- ThreadLocal的实现和使用场景
ThreadLocal 内部实现.应用场景和内存泄漏 深入理解线程局部变量:ThreadLocal <Java源码分析>:ThreadLocal /ThreadLocalMap Threa ...
- 阻止form表单提交的问题
阻止form表单提交这种场景可能在生活中,我们经常碰到,而在我们第一印象里面可能我们用return false 去阻止表单默认行为. 但是,有中情况我们用return false 不能阻止表单提交 & ...
- 9.20 session
2018-9-20 13:14:40 Cookie session 参考 :http://www.cnblogs.com/liwenzhou/p/8343243.html 类视图 cbv 注册 ur ...
- Save vtkMatrix4x4 to a file 保存到文件
vtkMatrix4x4是VTK中的一个表示4x4矩阵的一种数据结构,有时候我们想把其保存到一个文件中,那么可以使用如下的代码: void writeVtkMatrix4x4ToFile(const ...
- mysql max(),min()的优化
Select tables optimized away(选择表优化)
- Java与JS判断请求来是否来自移动端
Java public boolean JudgeIsMoblie(HttpServletRequest request) { boolean isMoblie = false; //String[] ...
- CentOS 6U7分区大于2TB的磁盘以及挂载大于16TB分区磁盘的解决方案
一.内容介绍1.问题描述1).问题一 CentOS 6.x 在格式化大于16TB的ext4分区时,会提示如下错误: mke2fs 1.41.12 (17-May-2010)mkfs.ext4: Siz ...
- java 中静态变量(类变量)与实例变量 静态方法与实例方法
静态变量: 在类内部中,方法的外部声明的对象而且加上static; 实例变量: 在类内部中,声明不需要加static; 比如: public class Demo{ int i1=0; static ...