Java Basics--shallow copy and deep copy

concept 

Shallow Copy and Deep Copy are commonly used concepts in the object copy process.

  • Shallow copy refers to creating a new object and copying the values ​​of non-static fields of the original object to the new object. If the field is a basic data type, its value is copied directly; if the field is a reference type, the reference is copied instead of the object itself. So, in a shallow copy, the old and new objects share the object pointed to by the reference type field.
  • Deep copy refers to creating a new object and copying the values ​​of all fields of the original object to the new object, including the object pointed to by the reference type field. Through deep copy, two independent objects can be realized without any relationship between each other. Modifying the field values ​​of one object does not affect the field values ​​of the other object.

Choosing to use shallow copy or deep copy depends on actual needs. If you want the modification of the copied object to not affect the original object, you should use deep copy; if you can accept the modification of the copied object to affect the original object, or the object is large and the copy performance requirements are high, you can use shallow copy.

In summary:

  1. shallow copy:

    • For simple objects such as primitive data types, their values ​​are copied directly.
    • For reference type fields, its reference is copied instead of the object itself.

    The result is that the same reference is shared between the original object and the copied object, and modifications to the original object will affect the copied object, but modifications to the object itself pointed to by the reference type field will not.

  2. Deep copy:

    • For simple object and reference type fields, independent copies of them are recursively created respectively, ensuring that the copied object is completely independent from the original object.

    The result is that the original object and the copied object do not have any reference relationship, exist independently of each other, and modifying one object does not affect the other.

It can be understood that a B is copied from A. At this time, I modify the object of A, and the object of B also changes. This is a shallow copy. On the contrary, I modified the object of A, but the object of B did not change accordingly, which is a deep copy.

Sample code:

//创建一个类Person
class Person {
 String name;
 int age;
 
 // 构造函数
 public Person(String name, int age) {
     this.name = name;
     this.age = age;
 }
 
 // 重写toString方法
 @Override
 public String toString() {
     return "Person [name=" + name + ", age=" + age + "]";
 }
}

public class myclass {
 public static void main(String[] args) {
     // 创建一个Person对象
     Person person1 = new Person("John", 25);
     
     // 浅拷贝示例
     Person person2 = person1; // 直接将person1的引用赋值给person2
     person2.age = 30; // 修改person2的属性,也会影响到person1
     
     System.out.println("浅拷贝示例:");
     System.out.println("person1: " + person1);//运行结果:person1: Person [name=John, age=30]
     System.out.println("person2: " + person2);//运行结果:person2: Person [name=John, age=30]
     
     // 深拷贝示例
     Person person3 = new Person(person1.name, person1.age); // 创建新的Person对象,将person1的属性值复制给person3
     person3.age = 35; // 修改person3的属性,不会影响到person1
     
     System.out.println("深拷贝示例:");
     System.out.println("person1: " + person1);//运行结果:person1: Person [name=John, age=30]
     System.out.println("person3: " + person3);//运行结果:person3: Person [name=John, age=35]
 }
}

Guess you like

Origin blog.csdn.net/m0_74293254/article/details/132266261