deep clone and shallow clone of clone java

Shallow copy (shallow clone): After copying an object, the variables of the basic data type are re-created, and the reference type points to the original object.

Deep copy (deep clone): After copying an object, whether it is a basic data type or a reference type, it is re-created. To put it simply, the deep copy is completed.

If the following two categories

import java.io.Serializable;

public class Teacher implements Cloneable,Serializable {
	private static final long serialVersionUID = 1L;
	private String name;
	private Integer age;
	private String sex;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
	
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Student implements Cloneable,Serializable {
	private static final long serialVersionUID = 1L;
	private String name;
	private int age;
	private String sex;
	private Teacher teacher;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	// clone
	public Student clone(){
		Student student = null;
		try {
			student = (Student) super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace ();
		}
		return student;
	}
	
	//deepClone();
	public Student deepClone() throws Exception{
		// write the object to the stream
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(baos);
		oos.writeObject(this);
		
		//read the object from the stream
		ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
		ObjectInputStream ois = new ObjectInputStream(bais);
		Student student = (Student) ois.readObject();
		ois.close();
		bais.close();
		oos.close();
		baos.close();
		return  student;
		
	}
	
}

  

 

New test class

public class TestCloneApp {
	public static void main(String[] args) throws Exception {
		
		Teacher teacher = new Teacher();
		teacher.setName("Teacher Zhang");
		teacher.setAge(53);
		teacher.setSex("男");
		
		
		Student student = new Student();
		student.setAge(5);
		student.setName("Zhang San");
		student.setSex("女");
		student.setTeacher(teacher);
		
		//clone
		Student cloneStudent = student.clone();
		
		cloneStudent.setName("Li Si");
		cloneStudent.setSex("男");
		cloneStudent.getTeacher().setName("Teacher Li");;
		//deepClone
		
		Student deepClone = student.deepClone();
		deepClone.setName("aaa");
		deepClone.setSex("bbb");
		deepClone.getTeacher().setName("cccc");
		System.out.println("person"+"------->"+"name: "+student.getName()+",age: "+student.getAge()+",sex: "+student.getSex()+",teacher: "+student.getTeacher().getName());
		System.out.println("clonePerson"+"------->"+"name: "+cloneStudent.getName()+",age: "+cloneStudent.getAge()+",sex: "+cloneStudent.getSex()+",teacher: "+cloneStudent.getTeacher().getName());
		System.out.println("deepClone"+"  ------->"+"name: "+deepClone.getName()+",age: "+deepClone.getAge()+",sex: "+deepClone.getSex()+",teacher: "+deepClone.getTeacher().getName());
		
	}
}

 

The output is as follows

student------->name: Zhang San, age: 5, sex: female, teacher: Mr. Li

cloneStudent------->name: Li Si,age: 5,sex: male,teacher: Mr. Li

deepClone  ------->name: aaa,age: 5,sex: bbb,teacher: cccc

 

Summarize: 

Shallow clone will copy an object, and the object property is a basic type will be reproduced, which is equivalent to copying. Instead, the reference type refers to the reference address of the original object. So when the cloneStudent object changes the teacher's name, the student object's teacher's name also changes.

 

A deep clone will also copy an object, but will make a new copy regardless of whether the object's properties are primitive types or reference types. So when deepClone changes any properties, the properties of the original object (student) will not be affected.

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326266061&siteId=291194637