Gradual analysis of shallow and deep copies of Java

Source: Internet
Author: User
Tags shallow copy

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:

public class Person {    private String name;    Private String sex;    private int age;        Public person (String name,string sex,int age) {        this.name = name;        This.sex = sex;        This.age = age;    }        Public person (person p) {                   //Copy construction method, copy object        this.name = p.name;        This.sex = P.sex;        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:

public class Asian {    private String skin;    person person;        Public Asian (String skin,person person) {        This.skin = skin;        This.person = person;                    Reference Assignment    } public    Asian (Asian Asian) {                 //Copy construction method, copy Object this        (Asian.skin,asian.person);               }}

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

For the person object there are the following:

person P1 = new Person ("John Doe", "Mam", 23); person P2 = 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:

Asian A1 = new Asian ("Yellow", New person ("John Doe", "Mam", 23)); Asian A2 = 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:

Gradual analysis of shallow and deep copies 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.