• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅

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.

posted @ 2014-09-18 06:13  neverlandly  阅读(571)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3