Fork me on GitHub

JAVA中的深拷贝与浅拷贝

Java中有两种类型变量,值类型和引用类型。

  • 对于值类型:copy是属于全盘复制,也就是深拷贝

  • 对于引用类型:一般的copy只是浅拷贝,相当于传递一个引用指针一样。

而当引用类型进行深拷贝的时候必须实现Cloneable接口中提供的Clone方法

通俗的说:

  • 深拷贝:不仅复制对象的基本类型,还复制原来对象中的对象,完全产生一个新的对象。

  • 浅拷贝:只是赋值对象的基本类型,对象还是属于原来的引用。如果是值类型直接拷贝到新的对象,引用类型则是复制一个引用到目标对象而已。

在有指针的情况下,浅拷贝只是增加了一个指针指向已经存在的内存而已。

深拷贝不但增加了一个指针还申请了一个新的内存,让这个增加的指针指向新的内存。所以当采用深拷贝释放内存的时候不会出现重复释放同一个内存的错误。

浅拷贝代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class Teacher implements Cloneable{
private String name;
private int age;

Teacher(String name,int age){
this.name=name;
this.age=age;
}

public Object clone() throws CloneNotSupportedException{
return super.clone();
}
}

public class Student implements Cloneable{
private String name;
private int age;
private Teacher teacher;

Student(String name,int age,Teacher teacher){
this.name=name;
this.age=age;
this.teacher=teacher;
}
//重写clone方法
public Object clone(){
Student student=null;
try{
student=(Student)super.clone();
}catch(CloneNotSupportedException e){
e.printStack();
}
return student;
}
}

//测试浅拷贝
public class ShallowCopy{
public static void main(String[] args){
Teacher teacher=new Teacher("流浪法师",50);
Student student1=new Student("盖伦",30,teacher);
Student student2=(Student)student1.clone();

student2.teacher.name="提莫";
student2.teacher.age=20;
student2.name="卡特";
student2.age=22;
System.out.println("student1的姓名:"+student1.name+",student1的老师名字:"
+student1.teacher.name+",student1的老师年龄:"+student1.teacher.age);//盖伦,提莫,20
//发现student2变了,student1变了,说明student1和student2的teacher引用是指向同一个对象。
}
}

深拷贝代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class Teacher implements Cloneable{
private String name;
private int age;

Teacher(String name,int age){
this.name=name;
this.age=age;
}

public Object clone() throws CloneNotSupportedException{
Object o=null;
try{
object=super.clone();//复制一个对象,相当于申请一片新的内存空间用来存储对象
}catch(CloneNotSupportedException e){
e.printStackTrace();
}
return object;
}
}

public class Student implements Cloneable{
private String name;
private int age;
private Teacher teacher;

Student(String name,int age,Teacher teacher){
this.name=name;
this.age=age;
this.teacher=teacher;
}
//重写clone方法
public Object clone(){
Student student=null;
try{
student=(Student)super.clone();
}catch(CloneNotSupportedException e){
e.printStack();
}
student.teacher=(Teacher)teacher.clone();//进行深拷贝
return student;
}
}

//测试深拷贝
public class DeepCopy{
public static void main(String[] args){
Teacher teacher=new Teacher("流浪法师",50);
Student student1=new Student("盖伦",30,teacher);
Student student2=(Student)student1.clone();

student2.teacher.name="提莫";
student2.teacher.age=20;
student2.name="卡特";
student2.age=22;
System.out.println("student1的姓名:"+student1.name+",student1的老师名字:"
+student1.teacher.name+",student1的老师年龄:"+student1.teacher.age);//盖伦,流浪法师,50
//发现student2变了,student1还是没变,
//说明student1和student2的teacher引用不是指向同一个对象。
//新增了一片存储空间,各自的引用指向了存储空间
}
}