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的更多相关文章

  1. [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. 这道题问 ...

  2. Deep Learning and Shallow Learning

    Deep Learning and Shallow Learning 由于 Deep Learning 现在如火如荼的势头,在各种领域逐渐占据 state-of-the-art 的地位,上个学期在一门 ...

  3. python deep copy and shallow copy

    Python中对于对象的赋值都是引用,而不是拷贝对象(Assignment statements in Python do not copy objects, they create bindings ...

  4. deep copy and shallow copy

    链接A:浅拷贝就是成员数据之间的一一赋值:把值赋给一一赋给要拷贝的值.但是可能会有这样的情况:对象还包含资源,这里的资源可以值堆资源,或者一个文件..当值拷贝的时候,两个对象就有用共同的资源,同时对资 ...

  5. python(41):copy拷贝(深拷贝deepcopy与浅拷贝copy)

    Python中的对象之间赋值时是按引用传递的,如果需要拷贝对象,需要使用标准库中的copy模块. 1.copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象. 2.copy.deepco ...

  6. UVa 10176 - Ocean Deep ! - Make it shallow !!

    题目大意:判断一个很大的二进制能否被131071整除.在二进制转十进制的过程中不断取模,最后判断结果是否是0就可以了. #include <cstdio> #include <cst ...

  7. python中的shallow copy 与 deep copy

    今天在写代码的时候遇到一个奇葩的问题,问题描述如下: 代码中声明了一个list,将list作为参数传入了function1()中,在function1()中对list进行了del()即删除了一个元素. ...

  8. 由Python的浅拷贝(shallow copy)和深拷贝(deep copy)引发的思考

    首先查看拷贝模块(copy)发现: >>> help(copy)Help on module copy:NAME    copy - Generic (shallow and dee ...

  9. iOS CMSampleBuffer deep copy

    extension CVPixelBuffer { func copy() -> CVPixelBuffer { precondition(CFGetTypeID(self) == CVPixe ...

随机推荐

  1. ubuntu 14.04 LTS 右键菜单解压压缩包时出错

    先卸载rar sudo apt-get remove rar 再安装unrar sudo apt-get install unrar

  2. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十五:FIFO储存模块(同步)

    实验十五:FIFO储存模块(同步) 笔者虽然在实验十四曾解释储存模块,而且也演示奇怪的家伙,但是实验十四只是一场游戏而已.至于实验十五,笔者会稍微严肃一点,手动建立有规格的储存模块,即同步FIFO.那 ...

  3. 算法题目-记hulu失败的实习面试

    1.对于数组A[0,1,2,3,4,...,k],求得0<=i < j < k,且使得A[j] - A[i]为最大值. 最简单也最容易想到的搜索两遍,即可得到答案.i的位置从起始至倒 ...

  4. Cracking the Coding Interview(String and array)

    1.1实现一个算法判断一个字符串是否存在重复字符.如果不能利用另外的数据结构又该如何实现? My solution: /** *利用类似一个hash table的计数 *然后检查这个hash tabl ...

  5. Spring加载xsd文件报错:because 1) could not find the document; 2) the document could not be read...

    Spring启动时加载xml文件的过程: spring在加载xsd文件时总是先试图在本地查找xsd文件(spring的jar包中已经包含了所有版本的xsd文件),如果没有找到,才会转向去URL指定的路 ...

  6. Spark2 Dataset行列操作和执行计划

    Dataset是一个强类型的特定领域的对象,这种对象可以函数式或者关系操作并行地转换.每个Dataset也有一个被称为一个DataFrame的类型化视图,这种DataFrame是Row类型的Datas ...

  7. ABP之创建实体

    ABP框架是一个非常庞大的框架,里面的东西有很多,那么如果我需要使用ABP进行项目的开发,具体的使用流程是怎样的呢?接下来将以一个简单的电影票管理“系统”为例子具体的实现一下. 一. 实体的创建 实体 ...

  8. 深圳MPD大会,五大专题一会尽享

    深圳MPD大会,五大专题一会尽享 2013年9月,深圳的高温将慢慢褪去,炎炎夏日也会变得稍微清凉一些.但9月It届的峰会活动却没有丝毫的锐减.9月7-8日深圳将迎来MPD大会2013的收官之站. MP ...

  9. Python:fromkeys()方法

    简介 Python 字典(Dictionary) fromkeys() 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值. 语法 fromkeys()方法语法: ...

  10. JS security

    下文本文将结合代码向读者展示常见这些 JavaScript 安全漏洞,以便读者在实际编码过程中注意到这些安全问题,及早规避这些风险. 基于 DOM 的跨站点脚本编制 我们都听说过 XSS(Cross ...