JAVA - light copy and deep copy

Posted by gamerzfuse on Tue, 08 Mar 2022 11:39:25 +0100

Understanding of java copy
In the java language, when we need to copy an object, there are two common ways to copy; Deep copy and shallow copy.
Shallow copy only copies the address of the original object, so when any value of the original object changes, the value of the copied object will also change.
Deep copy copies all the values of the source object instead of the address, so when the value of the source object changes, the value of the copy object will not change.
1: Shallow copy
Schematic diagram of shallow copy:

Let's do a basic demonstration:
Define a User class
public class User {

private String username = "Zhang San";
private String password = "123456";

public String getUsername() {
	return username;
}
public void setUsername(String username) {
	this.username = username;
}
public String getPassword() {
	return password;
}
public void setPassword(String password) {
	this.password = password;
}

}

Define a test class
public static void main(String[] args) {
User user01 = new User();
User user02 = user01;
user02.setUsername("Li Si");
System.out.println(user01.getUsername()); // Li Si
System.out.println(user02.getUsername()); // Li Si
System.out.println("-----------------------------------");
user01.setPassword("66666");
System.out.println(user01.getPassword()); //66666
System.out.println(user02.getPassword()); //66666
}

Output result:
Li Si
Li Si

66666
66666

When any value of user02 is changed, user01 will also be changed. Similarly, when any value of user01 is changed, user02 will also be changed.
2: Deep copy
Deep copy meaning map:

Several common deep copy methods:
Constructor mode
Override clone method
Apache Commons Lang serialization
Gson serialization
Jackson serialization
① : constructor mode
The new method allows each object to be newly created without interference with each other. However, the new method can be barely used when the number of objects is small. When too many objects are created, it will cost a lot of system overhead, so it is not recommended to complete this method.
② : override clone() method
1. Override the clone() method inherited from the parent class of Object and change it to public
public class User implements Cloneable{
2. Implement clonable interface to tell our jvm that copying is allowed
@Override
public User clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return (User)super.clone();
}

Changed user
public class User implements Cloneable{

private String username = "Zhang San";
private String password = "123456";

public String getUsername() {
	return username;
}
public void setUsername(String username) {
	this.username = username;
}
public String getPassword() {
	return password;
}
public void setPassword(String password) {
	this.password = password;
}

@Override
public User clone() throws CloneNotSupportedException {
	// TODO Auto-generated method stub
	return (User)super.clone();
}

}

Changed Test
public static void main(String[] args) {

	try {
		User user01 = new User();
		User user02 = user01.clone();
		System.out.println(user01);
		System.out.println(user02);
	} catch (CloneNotSupportedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

Printed results
kaobei.User@15db9742
kaobei.User@6d06d69c
Different address

But please note:
1. We add a new class Person
public class Person {
public String name ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2. Add in user class
Person person = new Person();

public void ChangedPerson(String name) {
	person.setName(name);
}

public String getPerson() {
	return person.getName();
}

Finally, the test class is tested
public static void main(String[] args) {

	try {
		User user01 = new User();
		User user02 = user01.clone();
		System.out.println(user01);
		System.out.println(user02);
		user01.ChangedPerson("Li Si");
		System.out.println(user01.getPerson());
		System.out.println(user02.getPerson());
	} catch (CloneNotSupportedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

The final result is
kaobei.User@15db9742
kaobei.User@6d06d69c
Li Si
Li Si

It is concluded that the deep copy method of overriding clone() method can only copy the current class, and cannot copy the class referenced by the current class.

Topics: Java Apache