Summary of shallow copy and deep copy of Java

Object copy in Java refers to copying all attributes (member variables) of an object to another object with the same class type. For example, object a and object B belong to class s and have attributes a and B. then copy object a and assign value to object B: B.A = A.A; B.B = A.B;

It is very common to copy objects in programs, mainly to reuse part or all of the data of existing objects in a new context.

Object copy in Java is mainly divided into shallow copy and deep copy.

First of all, let's introduce some foreshadowing knowledge: data types in Java are divided into basic data types and reference data types. For these two data types, there is a difference between value passing and reference (address) passing when performing assignment operation, being used as method parameter or return value.

Shallow copy (shallow copy): ① for a member variable whose data type is a basic data type, shallow copy will directly transfer the value, that is, copy the attribute value to a new object. Because it is two different data, modifying the member variable value of one object will not affect the data copied by the other object. ② for a data type, it is a reference If the member variable of a data type is an array or an object of a class, the shallow copy will be passed by reference, That is, just copy the reference value (memory address) of the member variable to the new object. In fact, the member variable of two objects points to the same instance. In this case, modifying the member variable in one object will affect the member variable value of another object.

The specific model is shown in the figure: you can see the member variables of the basic data type and create a new copy of their values. There is still only one instance of a member variable that references a data type, and the member variable of both objects points to the same instance.

There are three main ways to implement shallow copy:

1、 Shallow copy through copy construction method:

Copy constructor means that the constructor parameter of this class is the object of this class. Using the copy construction method can well complete the shallow copy, and directly create a new object with the same attributes as an existing object.

The code reference is as follows:

The object class is the root class of the class structure. One of the methods is protected object clone() throws clonenotsupportedexception, which is a shallow copy. With this shallow copy template, we can implement the shallow copy of objects by calling the clone () method. Note: 1. Although the object class has this method, However, this method is protected (modified by protected), so we can't use it directly. 2. The class using the clone method must implement the clonable interface, or an exception clonenotsupportedexception will be thrown. For these two points, our solution is to override the clone () method in the class using the clone method through super Clone() calls the original clone method in the object class.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>