What is the difference between copying and cloning?

Is there a final reference to this in programming?

I see that many people treat deep copying and cloning as one and the same. It's true?

Is he language dependent?

A little point, but it bothered me ...

+28
c # oop
Feb 04 '10 at 2:36 p.m.
source share
5 answers

There is no formal definition of these concepts, at least not one covering all languages.

What is usually usual:

  • clone - create something new based on something that exists.
  • copying - copying from something that exists into something else (it already exists).
+26
Feb 04 '10 at 14:50
source share

Yes, there is a difference. As for language dependencies, some languages ​​can make all shallow, deep, lazy copies. Some only make small copies. So yes, sometimes it depends on the language.

Now take, for example, an array:

int [] numbers = { 2, 3, 4, 5}; int [] numbersCopy = numbers; 

Now the numbersCopy array contains the same values, but more importantly, the array object itself points to the same object reference as the numbers array.

So, if I did something like:

  numbersCopy[2] = 0; 

What will be the output for the following statements?

  System.out.println(numbers[2]); System.out.println(numbersCopy[2]); 

Given that both arrays point to the same link, we get:

0

0

But what if we want to make a separate copy of the first array with its own link? In this case, we would like to clone the array. In addition, each array will have its own reference to the object. Let's see how this will work.

  int [] numbers = { 2, 3, 4, 5}; int [] numbersClone = (int[])numbers.clone(); 

Now the numbersClone array contains the same values, but in this case the array object itself indicates a different reference than the numbers array.

So, if I did something like:

  numbersClone[2] = 0; 

What will be the output for the following statements?

  System.out.println(numbers[2]); System.out.println(numbersClone[2]); 

You guessed it:

four

0

Source

+7
Feb 04 '10 at 14:47
source share

I would say that copying and cloning are similar terms. The only thing you should know is that you get a shallow copy and a deep copy. A shallow copy only copies the object at the root level, where a copy of the object and all its children will be created as a deep copy.

+2
04 Feb '10 at 14:40
source share

The shortest:

  • copy: copy an existing instance (shallow or deep)
  • clone: ​​copy to new instance (always deep)

Lack of consensus, as developers casually exchange them; however, one could lobby the above on the basis of:

  • Etymology (biology) implies that the concept of a “small clone” is meaningless because it is not genetically identical; cloning implies completeness for the distribution of an object.
  • Copying historically implies replication to an existing medium (copying a book or painting, etc.). For example, a photocopy copies the image onto an existing sheet of paper; if someone could clone a piece of paper, the result would be a new piece of paper.
  • You can "copy" a link to an object, but you will never "clone" a link to an object.
+2
May 26 '15 at 18:25
source share

In C ++, “cloning” the earth is usually an idiom for objects with deep copying of objects of polymorphic classes.

In Java / C #, I suspect that these terms are used more interchangeably.

+1
04 Feb '10 at 14:40
source share



All Articles