A brief analysis of deep copy and shallow copy of Java

Source: Internet
Author: User
Tags shallow copy

Tag: string Initializes an HTML image of www. Share TAC Attach

(Transferred from: http://www.cnblogs.com/chenssy/p/3308489.html)

First look at the definition of shallow copy and deep copy:

Shallow copy: Use a known instance to assign a value to the member variable of the newly created instance, which is called a shallow copy.

Deep copy: When a copy of a class is constructed, the value of all non-reference member variables of the object is not only copied, but also a new instance is created for the member variable of the reference type and initialized to the formal parameter instance value. This method is called deep copy

This means that a shallow copy copies only one object, passes a reference, and cannot replicate an instance. A deep copy copies the references inside the object, creates a new instance, and copies the instance.

For a shallow copy when the member variable of an object is the base data type, the member variable of two objects already has storage space, the assignment operation passes the value, so a shallow copy can replicate the instance. However, when the object's member variable is a reference data type, the object's replication cannot be implemented.

There is an object person with the following code:

1  Public classPerson {2     PrivateString name;3     PrivateString sex;4     Private intAge ;5     6      PublicPerson (String name,string sex,intAge ) {7          This. Name =name;8          This. Sex =sex;9          This. Age =Age ;Ten     } One      A      PublicPerson (person P) {//Copy construction methods, copying objects -          This. Name =P.name; -          This. Sex =P.sex; the          This. Age =P.age; -     } -}

The above object person has three member variables. Name, sex, age. Two methods of construction. The second parameter is the object, which is called the copy construction method, which initializes the new object created to the instance value of the formal parameter, which enables the object replication functionality.

There is also an object Asian, as follows:

1  Public classAsian {2     PrivateString Skin;3 person person ;4     5      PublicAsian (String skin,person person) {6          This. Skin =Skin;7          This. person = person;//Reference Assignment8     }9 Ten      PublicAsian (Asian Asian) {//Copy construction methods, copying objects One          This(Asian.skin,asian.person);  A     } -}

The above object also has two member variables, skin and person objects

For the person object there are the following:

1 New Person ("John Doe", "mam", +); 2 3 New Person (P1);

When The above statement is called. The P2 object will replicate to the P1. The implementation is as follows:  

  

for Asian objects are:

1 New Asian ("Yellow",new person ("John Doe", "Mam",)); 2 3 New Asian (A1);

New Asian (A1) executes the copy construction method of the Asian class, since the object assignment is a reference assignment. Enables A1 and A2 to reference the same object

Such as:

When A1 executes a statement that can change the value, A1 will also change the member variable of the A2 object through this statement

If you execute the following statement: A2.name = new Person (a1.name)

A new person object will be created.

Such as:

A brief analysis of deep copy and shallow copy of Java

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.