Is clone() in java shallow copy?

2020-02-26 08:53发布

Is clone() in java a shallow copy?

Eventually this gets to the clone() method of Object (the uppermost class), which creates a new instance of the same class as the object and copies all the fields to the new instance (a "shallow copy").

I read this from wikipedia.

I don't understand why it is a shallow copy. clone() will create a new instance with all fields. Is this just a deep copy? confused. Need some explanation for me.

8条回答
神经病院院长
2楼-- · 2020-02-26 09:03

What clone does is defined for each object that chooses to support clone. Object.clone is protected, so no object allows clone unless someone has specifically defined it.

查看更多
We Are One
3楼-- · 2020-02-26 09:04

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

public Object clone()

    Returns a shallow copy of this ArrayList instance. (The elements themselves are not copied.)
查看更多
三岁会撩人
4楼-- · 2020-02-26 09:10

As an aside, I'm surprised nobody has mentioned Joshua Bloch's views on Cloneable

If you've read the item about cloning in my book, especially if you read between the lines, you will know that I think clone is deeply broken. There are a few design flaws, the biggest of which is that the Cloneable interface does not have a clone method. And that means it simply doesn't work: making something Cloneable doesn't say anything about what you can do with it. Instead, it says something about what it can do internally. It says that if by calling super.clone repeatedly it ends up calling Object's clone method, this method will return a field copy of the original.

查看更多
啃猪蹄的小仙女
5楼-- · 2020-02-26 09:12

It is a shallow copy because it only copies reference to other objects. Say we have these classes :

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

class B { }

And now we make a clone of an instance of A :

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

The B instance in firstA and secondA will be the same. You won't have a copy of the B instance. This is why clone() is said to do shallow copy.

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

查看更多
相关推荐>>
6楼-- · 2020-02-26 09:12

The default implementation of Object.clone() is a shallow copy. This behavior is still useful for types that have a large number of primitive fields or Immutable fields. You can look at How to properly override clone method? for how to properly override it. After calling super.clone(), then casting the resulting object, you can then clone deeper as needed.

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

查看更多
仙女界的扛把子
7楼-- · 2020-02-26 09:13

clone() creates copy of all fields. Java have primitive types and refences - when you clone your object you get a new object with copies of all primitive field (it is like deep copy) but also you have copy of all refernce fields. So in result you get two objects with they own copies of primitives and copies of references to the same objects - both original and copied object will use the same objects.

查看更多
登录 后发表回答