Deep and shallow copies

catalogue

1, Object creation (both ways)

 1. new create object

2. clone replication object

2, Shallow copy

1. Definition

2. Code

3. Shallow copy summary

III. deep copy

1. Definition

2. Code

3. Summary

1, Object creation (both ways)

Before we talk about deep copy and shallow copy, we should know that deep copy and shallow copy are mainly for reference data, not basic data types. When we encounter reference data types, the first thing we think of is the creation of objects. How many ways can Java create objects?

The answer is: there are two ways to create objects: 1. Create objects with new; 2. Copy objects through clone.

Next, let's use some examples to analyze

1. new create object

public class Test {
	
	public static void main(String[] args) {
		Person person = new Person("Xiao Ming",18);
		Person otherPerson= person;
		person.print();
		otherPerson.print();
		System.out.println(person);
		System.out.println(otherPerson);
	}
}
class Person{
	private String name;
	private int age;
	public Person(String name,int age) {
		this.name = name;
		this.age = age;
	}
	public void print() {
		System.out.println("full name:"+name+",Age"+age);
	}
}

 

Let's analyze the results first. From the results, we can see that the addresses of the two are the same, so we can analyze that they are the same object, This object is the result of our output (Name: Xiao Ming, age 18), so we can get the conclusion that person and otherPerson only point to two references to the same object, which are called reference copies. Next, we can use a graph to deepen our understanding.

2. clone replication object

public class Test {
	
	public static void main(String[] args) throws CloneNotSupportedException {
		Person person = new Person("Xiao Ming",18);
//		Person otherPerson= person;
		Person otherPerson = (Person) person.clone();
		person.print();
		otherPerson.print();
		System.out.println(person);
		System.out.println(otherPerson);
	}
}
class Person implements Cloneable{
	private String name;
	private int age;
	public Person(String name,int age) {
		this.name = name;
		this.age = age;
	}
	public void print() {
		System.out.println("full name:"+name+",Age"+age);
	}
	public Object clone() throws CloneNotSupportedException {
		Object other =super.clone();
		return other;
		
	}
}

We can see from the results that the addresses of the two are different, so as to realize the real copy of an object.

2, Shallow copy

1. Definition

Shallow copy creates a new object that copies the original data. If the attribute is a basic type, the copied value is the value of the basic type. If it is a reference type, the copied value is the memory address. Therefore, if one object changes this address, it will affect the other object.

2. Code

public class Test {
	
	public static void main(String[] args) throws CloneNotSupportedException {
		Play play = new Play("motion");
		Person person = new Person("Xiao Ming",18);
		person.setPlay(play);
		Person otherPerson = (Person) person.clone();
		otherPerson.setname("Xiao Hong");
		otherPerson.print();
		play.setSport("Basketball");
		person.print();
		otherPerson.print();
	}
}
class Person implements Cloneable{
	private String name;
	private int age;
	private Play play;
	public Person(String name,int age) {
		this.name = name;
		this.age = age;
	}public void setname(String name) {
		this.name = name;
	}
	public void print() {
		System.out.println("full name:"+name+",Age"+age+"motion:"+this.getPlay().getSport());
	}
	public void setPlay(Play play) {
		this.play=play;
	}
	public Play getPlay() {
		return play;
	}
	public Object clone() throws CloneNotSupportedException {
		Object other =super.clone();
		return other;
		
	}
}
class Play{
	String sport;
	public Play(String sport) {
		this.sport=sport;
	}
	public void setSport(String sport) {
		this.sport=sport;
	}
	public String getSport() {
		return this.sport;
	}
}

 

3. Shallow copy summary

Point to different person objects from person and otherPerson respectively. When the data of the person pointed to by otherPerson is modified, it will not affect the person data of person, but when Play changes, it will affect both. Therefore, it can be deduced that they all point to the same Play class. I will use a diagram to facilitate your understanding.

III. deep copy

1. Definition

Deep copy copies all attributes and dynamically allocated memory pointed to by attributes. A deep copy occurs when an object is copied with the object it references.

2. Code

public class Test {
	
	public static void main(String[] args) throws CloneNotSupportedException {
		Play play = new Play("motion");
		Person person = new Person("Xiao Ming",18);
		person.setPlay(play);
		Person otherPerson = (Person) person.clone();
		otherPerson.setname("Xiao Hong");
		otherPerson.print();
		play.setSport("Basketball");
		person.print();
		otherPerson.print();
	}
}
class Person implements Cloneable{
	private String name;
	private int age;
	private Play play;
	public Person(String name,int age) {
		this.name = name;
		this.age = age;
	}public void setname(String name) {
		this.name = name;
	}
	public void print() {
		System.out.println("full name:"+name+",Age"+age+"motion:"+this.getPlay().getSport());
	}
	public void setPlay(Play play) {
		this.play=play;
	}
	public Play getPlay() {
		return play;
	}
	public Object clone() throws CloneNotSupportedException {
		Person person = (Person) super.clone();
		person.play=(Play) this.play.clone();
		return person;
		
	}
}
class Play implements Cloneable{
	String sport;
	public Play(String sport) {
		this.sport=sport;
	}
	public void setSport(String sport) {
		this.sport=sport;
	}
	public String getSport() {
		return this.sport;
	}
	public Object clone() throws CloneNotSupportedException {
		Object other =super.clone();
		return other;
	}
}

 

3. Summary

Point to different person objects from person and otherPerson respectively. When the data of the person pointed to by otherPerson is modified, it will not affect the person data of person, but when the Play class of person changes, it will only affect person and not otherPerson. Therefore, it can be deduced that they point to different Play classes respectively. I will use a diagram to facilitate your understanding.

 

Keywords: Java

Added by andrewgauger on Mon, 27 Dec 2021 02:15:23 +0200