1.概念
复制都是在堆中开辟一段新的空间,两者不同在于类中变量是否也是重新开辟的一段内存,还是说用的引用类型,java中的数据类型分为基础数据类型和引用数据类型,基础数据类型包括byte、short、int、long、char、float、double,引用类型包括类,接口,数组类型和null类型。
1.1.浅复制
对于基础数据类型而言,只是值的复制;对于引用数据类型而言,只进行引用的拷贝,并未new一个新对象
1.2.深复制
对于基础数据类型而言,也只是值的复制;对于引用数据类型而言,重新new了一个新对象,并复制其内容
2.代码验证
2.1.Java中实现类之间复制的方式
在Java中,不管哪种方式的复制,都是通过继承Object,并实现其中的clone()方法
* @return a clone of this instance.
* @throws CloneNotSupportedException if the object's class does not
* support the {@code Cloneable} interface. Subclasses
* that override the {@code clone} method can also
* throw this exception to indicate that an instance cannot
* be cloned.
* @see java.lang.Cloneable
*/
protected native Object clone() throws CloneNotSupportedException;
翻译过来就是,如果我们想让这个类被克隆就必须实现Cloneable接口,不然会报CloneNotSupportedException异常
2.2.浅拷贝代码测试
一共两个类,主类以及TestCopy引用类
public class TestCopy {
}
public class Person implements Cloneable{
public int age;
public TestCopy testCopy;
public Person(int age,TestCopy testCopy){
this.age=age;
this.testCopy=testCopy;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public static void main(String[] args) throws CloneNotSupportedException {
//创建一个引用类
TestCopy testCopy=new TestCopy();
//创建一个本体类
Person p1=new Person(12,testCopy);
//复制一个类
Person p2=(Person)p1.clone();
System.out.println("--------p1---------------");
System.out.println(p1.hashCode());
System.out.println(p1.age);
System.out.println(p1.testCopy.hashCode());
System.out.println("--------p2---------------");
System.out.println(p2.hashCode());
System.out.println(p2.age);
System.out.println(p2.testCopy.hashCode());
}
}
测试结果如下:
--------p1---------------
21685669
12
2133927002
--------p2---------------
1836019240
12
2133927002
Process finished with exit code 0
从结果可以看出,克隆的类逻辑地址p2.hashCode()和主类p1.hashCode()不同,证明是新开辟的空间,两个基础类型的值相同,但是引用类型的hashCode是一样的,证明只是拷贝了一个引用。
2.3.深拷贝
主要有两种方式:
1.序列化再反序列化
2.对类中的引用类型进行clone()拷贝出一个新的再赋值
public class TestCopy implements Cloneable{
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Person implements Cloneable{
public int age;
public TestCopy testCopy;
public Person(int age,TestCopy testCopy){
this.age=age;
this.testCopy=testCopy;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Person person=(Person)super.clone();
person.testCopy= (TestCopy) this.testCopy.clone();
return person;
}
public static void main(String[] args) throws CloneNotSupportedException {
//new一个类中的引用对象
TestCopy testCopy=new TestCopy();
//new一个本体对象
Person p1=new Person(12,testCopy);
Person p2=(Person)p1.clone();
System.out.println("--------p1---------------");
System.out.println(p1.hashCode());
System.out.println(p1.age);
System.out.println(p1.testCopy.hashCode());
System.out.println("--------p2---------------");
System.out.println(p2.hashCode());
System.out.println(p2.age);
System.out.println(p2.testCopy.hashCode());
}
}
测试结果
--------p1---------------
21685669
12
2133927002
--------p2---------------
1836019240
12
325040804
Process finished with exit code 0
从结果可以看出,克隆的类地址p2.hashCode()和主类p1.hashCode()不同,证明是新开辟的空间,两个基础类型的值相同,引用类型的hashCode也不一样的,证明完成了深拷贝。