Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between copying and cloning?

Tags:

c#

oop

People also ask

Is it better to clone or copy a hard drive?

If the files are just data files (documents, pictures, etc.) there isn't any need to clone the drive, a simple copy will do. However, if you're trying to copy a drive that has an operating system on it (some version of Windows) and you want the copied drive to be able to boot up, then you need to clone the drive.

What is the difference between copy and clone in UVM?

clone method works exactly the same as a copy method, the difference being that a clone will return an object with the copied contents. So this saves some trouble of creating the second object before copy.

What is the difference between duplicate and clone image?

Over time, portions (or “fragments”) of files are spread out over the entire hard disk, with free space in between. A clone is an exact duplicate of the original. Not only is all the overhead and data copied, but the free space is also copied. This preserves the exact layout and organization of everything.

What is copy and clone in C#?

The clone is of the same Type as the original Array. The CopyTo() method copies the elements into another existing array. It copies the elements of one array to another pre-existing array starting from a given index (usually 0). Both perform a shallow copy .


There's no formal definition of these concepts, atleast not one that spans all languages.

What's usually common though:

  • clone - create something new based on something that exists.
  • copying - copy from something that exists to something else (that also already exists).

Yes, there is a difference. As far as language dependencies, some languages can do all Shallow, Deep, Lazy copying. Some only do Shallow copies. So yes, it is language dependent sometimes.

Now, take for instance an Array:

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

The “numbersCopy” array now contains the same values, but more importantly the array object itself points to the same object reference as the “numbers” array.

So if I were to do something like:

  numbersCopy[2] = 0;

What would be the output for the following statements?

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

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

Considering both arrays point to the same reference we would get:

0

0

But what if we want to make a distinct copy of the first array with its own reference? Well in that case we would want to clone the array. In doing so each array will now have its own object reference. Let’s see how that will work.

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

  int [] numbersClone = (int[])numbers.clone();

The “numbersClone” array now contains the same values, but in this case the array object itself points a different reference than the “numbers” array.

So if I were to do something like:

  numbersClone[2] = 0;

What would be the output now for the following statements?

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

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

You guessed it:

4

0

Source


Most concise:

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

No consensus as developers sloppily interchange them; however one could lobby the above based on:

  1. Etymology (Biology) implies that the notion of a "shallow clone" is nonsensical since not genetically identical; cloning implies completeness in order to propagate the entity.
  2. Copying historically implies replication onto existing medium (copying a book or painting, etc.) E.g., a photocopy copies an image onto an existing piece of paper; if one could somehow clone a piece of paper the result would be a new piece of paper.
  3. One could "copy" an object reference but one would never "clone" an object reference.

I would say that copy and cloning are analogous terms. The only thing that you should maybe be aware is that you get shallow copy and deep copy. Shallow copy only makes a copy of an object at the root level where as deep copy will produce a copy of an object and all its child objects.


In C++-land "cloning" is usually idiom for deep copying polymorphic classes' objects.

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