Java cloning--the difference between deep cloning and shallow cloning

Source: Internet
Author: User
Tags shallow copy

Cloning is copying a copy of an object, and cloning is a shallow clone and a deep clone. Shallow cloning means that the value of the base type of the cloned object has changed, and the value of the source object does not change. However, if the value of the reference type of the cloned object changes, the value of the source object is also changed because the reference type holds a reference address in the stack memory, and the object is cloned to the same memory space as the reference address of the source object. So at the time of cloning, any change in the value of any object will change the value of the other, so deep cloning will be used in this case.

Note that the tears of the objects to be cloned must inherit the Cloneable interface. The characteristic of shallow cloning is to clone only the object ontology, its advantages and disadvantages is a change of all; deep cloning is a new object, and the source object is not interfering with each other, and its advantages and disadvantages are non-interference and trouble. Usually we use only shallow clones when cloning is used.

Cloning can be easily understood as: When cloning an object, the value of the property and methods are copied together is a shallow copy, and the value of the method can be set different is a deep clone. Deep cloning to instantiate, first output the content to memory, and then read from memory.

Java cloning--the difference between deep cloning and shallow cloning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.