Summary: Deep copy vs. Shallow copy vs. Lazy copy

Source: Internet
Author: User

Object copy

AnObject copyIs 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
// Object E refers.
}
}

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.

 

 

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.