How does clone work under the hood?

后端 未结 4 666
小鲜肉
小鲜肉 2020-12-02 02:05

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

4条回答
  •  一整个雨季
    2020-12-02 02:26

    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

提交回复
热议问题