Clone of Java.lang.Object () deep copy, shallow copy

Source: Internet
Author: User

When a function's argument is an object, the reference type is passed, and the operation inside the object function affects the outer object. If you want to pass a copy of an object, then you need to use the Clone () method of the objects. The Copy object returns a new object, not a reference; the difference between a copy object and a new object returned by the newly operator is that the Copy object already contains some information about the original object, not the object's initial information. When to use shallow clone (shallow copy, shadow copy), when to use deep clone (Dark copy): Mainly see what the nature of the specific domain object is, if it is the base type using shallow Clone, if it is reference Variable (reference variable) uses deep clone.

In clone, a shallow copy and a deep copy, a shallow copy is a direct call to Super.clone () for copying regardless of whether the parameter is a basic type or a reference type, and the resulting reference type still points to the same memory; deep copy is after calling Super.clone (). Also call the Clone () function of the reference parameter to copy it to achieve a deep copy.

Shallow copy:

(1) A basic data type (String) is a copy of a copy of a new object

(2) refers to an object or refers to the same object.

Clone of Java.lang.Object () deep copy, shallow copy

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.