Is clone () in java a shallow copy?

Is clone() in java a shallow copy?

In the end, it gets to the clone () method of the object (the topmost class), which creates a new instance of the same class as the object and copies all the fields to the new instance ("shallow copy").

I read this from wikipedia .

I do not understand why this is a shallow copy. clone() will create a new instance with all fields. Is this just a deep copy? embarrassed. I need an explanation.

+7
source share
8 answers

By default, Object.clone() indeed a shallow copy. However, it is intended to throw a CloneNotSupportedException if your object does not implement Cloneable .

And when you implement Cloneable , you must override clone() to make a deep copy by calling clone() for all fields that are themselves cloned.

+13
source

As an aside, I am surprised that no one mentioned Joshua Bloch Looks at Cloneable

If you read the article on cloning in my book, especially if you read between the lines, you will know that I think the clone is deeply broken. There are several design flaws there, the biggest of which is that the Cloneable interface does not have a cloning method. And that means it just doesn't work: something Cloneable doesn't say anything about what you can do with It. Instead, he says something about what he can do internally. It says that if a call to super.clone repeatedly ends with a call to Object clone, this method will return a field copy of the original.

+5
source

clone () creates a copy of all fields. Java has primitive types and attributes - when you clone your object, you get a new object with copies of the entire primitive field (this looks like a deep copy), but you also have a copy of all refernce fields. Thus, you get two objects with their own copies of primitives and copies of links to the same objects - both the original and copied objects will use the same objects.

+4
source

Some objects do not provide a deep copy. For example, an ArrayList will clone a list, but not elements in the list. Following is the JavaDoc for ArrayList:

 public Object clone() Returns a shallow copy of this ArrayList instance. (The elements themselves are not copied.) 
+3
source

That clone really defined for every object that wants to support the clone. Object.clone is protected, so no object permits cloning unless someone specifically defines it.

+1
source

This is a shallow copy because it only copies links to other objects. Let's say we have these classes:

 class A { B variable A() { variable = new B(); } } class B { } 

And now we create a clone of instance A:

 A firstA = new A(); A secondA = firstA.clone(); 

Instance B in firstA and secondA will be the same. You will not have a copy of instance B. That's why clone () is called a shallow copy.

The diagrams on the linked page should help you understand all this.

+1
source

The default implementation of Object.clone () is a shallow copy. This behavior is still useful for types with many primitive fields or immutable fields. You can take a look at How to override the clone method correctly? to override it correctly. After calling super.clone (), then casting the resulting object, you can then clone deeper as needed.

Implicitly, the value of the clone decreases as the number of complex mutable fields on your type increases.

+1
source

Yes.

But first you need your class to implement Cloneable and throw exception

 class A implements Cloneable{ public int y; public B b; public A(){ b = new B(); } public static void main(String[] args) throws CloneNotSupportedException{ A a = new A(); A a2 = (A) a.clone(); System.out.print(ab==a2.b); } } 

Output: true

0
source

All Articles