Analysis of java's shortest copy and deep copy

Source: Internet
Author: User

First, let's take a look at the definition of the shortest copy and deep copy:

Shallow copy: assign values to the member variables of the newly created instance one by one using a known instance. This method is called shortest copy.

Deep copy: When a copy constructor of a class not only copies all non-referenced member variable values of an object, but also creates a new instance for referenced member variables, the instance value is initialized as a form parameter. This method is called Deep copy.

That is to say, the shortest copy only copies one object and transmits references. The instance cannot be copied. Deep COPY Copies all references to the object. It creates a new instance and copies the instance.

For the shortest copy, when the object's member variables are of the basic data type, the member variables of the two objects already have a bucket. The value assignment operation passes the value, so the shortest copy can copy instances. However, when the object's member variables are of the reference data type, the object cannot be copied.

There is an object Person. The Code is as follows:

   Person(String name,String sex,.name =.sex =.age = Person(Person p){                           .name =.sex =.age =

The above object Person has three member variables. Name, sex, age. Two constructor methods. The second parameter is the object, which is called the copy constructor. It initializes the new object to the Instance value of the form parameter and enables object replication.

Another object, Asian, is as follows:

 .skin =.person = person;                     Asian(Asian asian){                         

The preceding object also has two member variables: skin and Person.

The person object is as follows:

Person p1 = Person ("Li Si", "mam", 23 = Person (P1 );

When the preceding statement is called. P2 object will copy P1. The execution is as follows:

For Asian objects:

Asian a1 = Asian ("yellow", Person ("Li Si", "mam", 23 = Asian (a1 );

New Asian (a1) executes the Asian class copy construction method, because the object Value assignment is a reference value assignment. So that a1 and a2 reference the same object

For example:

When a1 executes a statement that can change this value, a1 can use this statement to change the member variables of the a2 object.

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

A new Person object will be created.

For example:

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.