1. The shallow copy

Create a new object that has exactly the same attributes as the original object, but for non-primitive attributes, points to the memory address of the object to which the original attribute points

1.1 Implementation of shallow copy

1.1.1 Implementing the Clone Interface

The Object parent class has a clone() copy, but it is protected and we need to override it and make it public. In addition, subclasses need to implement the Cloneable interface to tell the JVM that the class is copiable

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * Description:
 *
 * @author jack
 * @date2021/8/4 3:42pm */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable.Cloneable {

    private static final long serialVersionUID = -1286423671768591169L;

    private String name;

    private Integer age;

    private Address address;

    @Override
    protected Object clone(a) throws CloneNotSupportedException {
        return super.clone(); }}Copy the code
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * Description:
 *
 * @author jack
 * @date2021/8/4 3:42pm */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Address implements Serializable , Cloneable {

    private static final long serialVersionUID = -2220826382327087863L;

    private String country;

    private String city;
    
    @Override
    protected Object clone(a) throws CloneNotSupportedException {
        return super.clone(); }}Copy the code

test

/**
 * Description:
 *
 * @author jack
 * @date2021/8/4 3:44pm */
public class CloneTest2 {
    public static void main(String[] args) throws CloneNotSupportedException {
        User user1 = User.builder()
                .name("Zhang")
                .age(18)
                .address(Address.builder()
                        .country("China")
                        .city("Shanghai")
                        .build())
                .build();

        User user2 = (User) user1.clone();

        System.out.println(user1 == user2); // false
        System.out.println(user1.getAddress() == user2.getAddress()); // true}}Copy the code

1.1.2 useBeanUtils.copyProperties

import org.springframework.beans.BeanUtils;

/**
 * Description:
 *
 * @author jack
 * @date2021/8/4 3:44pm */
public class CloneTest2 {
    public static void main(String[] args) throws CloneNotSupportedException {
        User user1 = User.builder()
                .name("Zhang")
                .age(18)
                .address(Address.builder()
                        .country("China")
                        .city("Shanghai")
                        .build())
                .build();
        User user2 = new User();
        BeanUtils.copyProperties(user1, user2);

        System.out.println(user1 == user2); // false
        System.out.println(user1.getAddress() == user2.getAddress()); // true}}Copy the code

2. Deep copy

When a new object is created, other objects referenced in the property will be cloned and no longer point to the original object address.

2.1 Implementation of deep copy

2.1.1 to useSerializationUtils.clone

You need to rely on commons-lang3

<! -- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.11</version>
</dependency>
Copy the code

Serializationutils.clone requires the class to implement the Serializable interface

import org.apache.commons.lang3.SerializationUtils;

/**
 * Description:
 *
 * @author jack
 * @date2021/8/4 3:44pm */
public class CloneTest {
    public static void main(String[] args) {
        User user1 = User.builder()
                .name("Zhang")
                .age(18)
                .address(Address.builder()
                        .country("China")
                        .city("Shanghai")
                        .build())
                .build();

        User user2 = SerializationUtils.clone(user1);

        System.out.println(user1 == user2); // false
        System.out.println(user1.getAddress() == user2.getAddress()); //false}}Copy the code