Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does clone work under the hood?

Tags:

java

clone

Clone does not call the object constructor to create a copy of the object. So what algorithm does clone use ?

I am looking for implementation details of the native method clone. Any pointers will be appreciated.

Note that I am aware of the shortcomings of clone.

like image 853
Vinoth Kumar C M Avatar asked Jul 26 '11 06:07

Vinoth Kumar C M


People also ask

How do clones work internally?

clone() is getting called inside clone() . As we know, clone() is declared in Object , so it is inherited by every Java object. Calling super. clone() copies our superclass' fields and makes bitwise copies of the fields.

Where does git clone save to?

The git clone command creates a copy of a remote repository on your local machine. By default, the clone command saves your code in a folder that shares the name of your repository. This can be overwritten by specifying a folder name after the URL of the repository you want to clone.

How do I clone a git repository?

On GitHub.com, navigate to the main page of the repository. Above the list of files, click Code. Click Open with GitHub Desktop to clone and open the repository with GitHub Desktop. Follow the prompts in GitHub Desktop to complete the clone.

How do I clone a specific folder from a git repository?

If you want to clone the git repository into the current directory, you can do like: $ git clone <repository> . Here, the dot (.) represents the current directory.


3 Answers

protected native Object clone(). I don't know exactly (I need to take a look at the native code) but it makes a new instance of the object inside the JVM and copies all fields.

But you should avoid using clone() - it is hard to get it right. Look at this question for more details

like image 85
Bozho Avatar answered Oct 31 '22 17:10

Bozho


How it works is laid out in the Javadoc:

The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.

E.g., a naive, shallow field-by-field copy, very nearly (but probably not quite) just a bit-for-bit copy of the object.

I am looking for implementation details of the native method clone.

That will vary from JVM implementation to JVM implementation. It's likely to be quite an efficient operation, though, if that's your concern.

like image 3
T.J. Crowder Avatar answered Oct 31 '22 17:10

T.J. Crowder


In terms of JNI, clone is (or could be) implemented using the AllocObject method which creates a new object without invoking any constructor (as opposed to NewObject). When you have the new object, reflection is used to shallowly copy all fields.

But then again, the clone/Clonable mechanism is fundamentally broken in Java. Joshua Bloch has a section about it in Effective Java. There is also several related SO questions about it.

like image 1
JesperE Avatar answered Oct 31 '22 17:10

JesperE