Reference copy, shallow copy, deep copy in Java

1. Preface Basic knowledge

1. Java data type: basic type, reference type
2. Basic type: the saved value itself
3. Reference data type: the saved point to the data address in the heap memory

2. Definition

Object copy refers to copying all properties (member properties) of an object to an object of the same type

Classification : reference copy, shallow copy, deep copy (deep copy and shallow copy are object copies)

3. Reference copy
Reference copy just copies the reference address, pointing to the same object

/**
 * @Author charles.yao
 * @Description 用户信息
 * @Date 2023/2/13 14:30
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
public class UserInfo {
    private String userName;
    private String userAge;
}

/**
 * @Author charles.yao
 * @Description 引用拷贝测试
 * @Date 2023/2/13 14:31
 */
public class QuoteCopyTest {
    public static void main(String[] args) {
        UserInfo userInfo = new UserInfo("张三", "18");
        UserInfo userCopy = userInfo;
        System.out.println("原用户"+userInfo.toString());
        System.out.println("引用拷贝"+userCopy.toString());
        System.out.println("原用户与引用拷贝比较"+userCopy.equals(userInfo));
    }
}

operation result:

原用户UserInfo(userName=张三, userAge=18)
引用拷贝UserInfo(userName=张三, userAge=18)
原用户与引用拷贝比较true

image-20230213153111158

4. Shallow copy

All variables of the copied object contain the same values ​​as the original object, and all references to other objects still point to the original object. That is, a shallow copy of an object will make a copy of the "main" object, but will not copy the objects inside the main object. The "object inside" will be shared between the original object and its copy.

code example


/**
 * @Author charles.yao
 * @Description 浅拷贝类
 * @Date 2023/2/13 14:53
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
public class UserInfo implements Cloneable{
    private String userName;
    private String userAge;
    private Animal animal;
    @Override
    protected UserInfo clone() throws CloneNotSupportedException {
        return (UserInfo) super.clone();
    }
}



/**
 * @Author charles.yao
 * @Description
 * @Date 2023/2/13 14:54
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Animal implements Cloneable {
    private String userName;
    private String userAge;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}


/**
 * @Author charles.yao
 * @Description 浅拷贝测试
 * @Date 2023/2/13 14:57
 */
public class ShallowCopyTest {
    public static void main(String[] args) {
        try {
            UserInfo userInfo = new UserInfo();
            userInfo.setUserName("张三");
            userInfo.setUserAge("1223");
            userInfo.setAnimal(new Animal("111", "2122"));
            UserInfo clone = userInfo.clone();
            System.out.println("原数据" + userInfo.toString());
            System.out.println("拷贝之后" + clone.toString());
            userInfo.setUserName("二狗");
            System.out.println("修改后原数据" + userInfo.toString());
            System.out.println("修改后拷贝之后" + clone.toString());
            System.out.println(userInfo == clone);
            System.out.println(userInfo.getAnimal() == clone.getAnimal());
            userInfo.getAnimal().setUserName("二狗");

            System.out.println("原数据动物" + userInfo.getAnimal().toString());
            System.out.println("拷贝之后动物" + clone.getAnimal().toString());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

operation result

原数据UserInfo(userName=张三, userAge=1223, animal=Animal(userName=111, userAge=2122))
拷贝之后UserInfo(userName=张三, userAge=1223, animal=Animal(userName=111, userAge=2122))
修改后原数据UserInfo(userName=二狗, userAge=1223, animal=Animal(userName=111, userAge=2122))
修改后拷贝之后UserInfo(userName=张三, userAge=1223, animal=Animal(userName=111, userAge=2122))
false
true
原数据动物Animal(userName=二狗, userAge=2122)
拷贝之后动物Animal(userName=二狗, userAge=2122)

image-20230213160247424

6. Deep copy

A deep copy is an entire independent object copy. A deep copy will copy all attributes and copy the dynamically allocated memory pointed to by the attributes. A deep copy occurs when an object is copied along with the objects it refers to. Deep copies are slower and more expensive than shallow copies.

code example


/**
 * @Author charles.yao
 * @Description 浅拷贝类
 * @Date 2023/2/13 14:53
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
public class UserInfo implements Cloneable {
    private String userName;
    private String userAge;
    private Animal animal;

    @Override
    protected UserInfo clone() throws CloneNotSupportedException {

        UserInfo userInfo = (UserInfo) super.clone();
        userInfo.setAnimal((Animal) animal.clone());
        return userInfo;
    }
}


/**
 * @Author charles.yao
 * @Description
 * @Date 2023/2/13 14:54
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Animal implements Cloneable {
    private String userName;
    private String userAge;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}


/**
 * @Author charles.yao
 * @Description 浅拷贝测试
 * @Date 2023/2/13 14:57
 */
public class DeepCopyTest {
    public static void main(String[] args) {
        try {
            UserInfo userInfo = new UserInfo();
            userInfo.setUserName("张三");
            userInfo.setUserAge("1223");
            userInfo.setAnimal(new Animal("111", "2122"));
            UserInfo clone = userInfo.clone();
            System.out.println("原数据" + userInfo.toString());
            System.out.println("拷贝之后" + clone.toString());
            System.out.println(userInfo == clone);
            System.out.println(userInfo.getAnimal() == clone.getAnimal());
            userInfo.getAnimal().setUserName("二狗");

            System.out.println("原数据动物" + userInfo.getAnimal().toString());
            System.out.println("拷贝之后动物" + clone.getAnimal().toString());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

Test Results

原数据UserInfo(userName=张三, userAge=1223, animal=Animal(userName=111, userAge=2122))
拷贝之后UserInfo(userName=张三, userAge=1223, animal=Animal(userName=111, userAge=2122))
false
false
原数据动物Animal(userName=二狗, userAge=2122)
拷贝之后动物Animal(userName=111, userAge=2122)

image-20230213161224941

Guess you like

Origin blog.csdn.net/CharlesYooSky/article/details/129011195