JAVA 浅拷贝和深拷贝

拷贝

拷贝即对已有的数据创建一个副本,在 Java 中,拷贝可分为引用拷贝、浅拷贝、深拷贝。

引用拷贝

在 Java 中,实例化后的对象存储在堆区,而局部变量存放在局部变量表(栈)中,如:

public void yinYongCopy() { 
    User user = new User("xiaoming", 12); 
}

user 变量只是存放堆区 User 实例的引用(地址)。那么把 user 赋值另一个变量 copy,称为引用拷贝(相当于地址赋值)。

public void yinYongCopy() { 
    User user = new User("xiaoming", 12); 
    User copy = user; 
}

user 和 copy 两个变量存放了 User 实例的指向,他们指向了同一份堆内存空间。

JAVA 浅拷贝和深拷贝_第1张图片

浅拷贝

在 java 中,默认的拷贝指的是类重写 Object 类中的 clone 方法,调用该方法进行拷贝。Object 类中的 clone 方法使用 Native 修饰,由本地方法实现,对于类型只是做引用拷贝。

public class User implements Cloneable {
    private String name;
    private Integer age;
    private Animal animal;
    
    get...set...

    @Override
    protected User clone() throws CloneNotSupportedException {
        return (User) super.clone();
    }
}

public class Animal implements Cloneable {
    private String name;
    private Integer age;

    get..set..

    @Override
    protected Animal clone() throws CloneNotSupportedException {
        return (Animal) super.clone();
    }
}

public class Copy {
    public static void main(String[] args) throws CloneNotSupportedException {
        User user = new User();
        user.setAge(10);
        user.setName("xiaoming");
        user.setAnimal(new Animal("wangcai", 10));
        System.out.println(user);
        User clone = user.clone();
        System.out.println(clone);
        System.out.println(user == clone);
        System.out.println(user.getAnimal() == clone.getAnimal());
    }
}

使用 clone 方法生生成新的对象,此时在堆中产生新的对象,clone 存放了该对象的引用。但是需要注意,此时 user 和 copy 中的 animal 指向堆空间中同一个实例,即只是做了引用的拷贝。

JAVA 浅拷贝和深拷贝_第2张图片

深拷贝

深拷贝相对于浅拷贝而言,会对所有数据类型都进行拷贝一份,包括这个对象所包含的内部对象。java 中上实现深拷贝的方式有两种:

  • 对浅拷贝进行加强,对象中引用属性进行 clone 操作;
  • 通过序列化;
  • 借助三方工具类;

浅拷贝加强

对浅拷贝进行加强,对象中引用属性进行 clone 操作,以下例子 User 类 clone 的时候也对 animal 进行拷贝。

public class User implements Cloneable {
    private String name;
    private Integer age;
    private Animal animal;
    
    get..set...

    @Override
    protected User clone() throws CloneNotSupportedException {
        User clone = (User) super.clone();
        clone.setAnimal(animal.clone());
        return clone;
    }
}

public class Copy {
    public static void main(String[] args) throws CloneNotSupportedException {
        User user = new User();
        user.setAge(10);
        user.setName("xiaoming");
        user.setAnimal(new Animal("wangcai", 10));
        System.out.println(user);
        User clone = user.clone();
        System.out.println(clone);
        System.out.println(user == clone);
        System.out.println(user.getAnimal() == clone.getAnimal());
    }
}

序列化

User 类实现 Serializable 接口,对堆区中的实例进行读写,从而实现对实例的全部拷贝。

public class User implements Serializable, Cloneable {
    private String name;
    private Integer age;
    private Animal animal;

    get...set...

    @Override
    protected User clone() {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(this);
            oos.flush();
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
            return (User) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
}

借助三方工具类

例如使用 fastJson 工具对对象实现深度拷贝。

@Override
protected User clone() {
    String s = JSON.toJSONString(this);
    return JSON.parseObject(s, User.class);
}

你可能感兴趣的:(JAVA,java,jvm,servlet)