Java浅拷贝与深拷贝

本文介绍了如何在Java中使用Spring框架的BeanUtils和Cglib的BeanCopier进行浅拷贝,以及深拷贝(通过序列化反序列化)的概念。实验展示了浅拷贝只复制对象引用,而深拷贝创建了完全独立的对象实例。
摘要由CSDN通过智能技术生成

一、浅拷贝(Shallow Copy)

springframework和cglib性能比较好
https://developer.aliyun.com/ask/315548

package com.ityj.algorithm.clone;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.cglib.beans.BeanCopier;

@Slf4j
public class ShallowCopyTest {

    // https://www.infoworld.com/article/2077578/how-to-copy-objects-in-java-shallow-copy-and-deep-copy.html
    //https://www.bilibili.com/video/BV1Sa41127pU/?spm_id_from=333.337.search-card.all.click&vd_source=b23569b676ce26126febad3c290b16e8
    // https://zhuanlan.zhihu.com/p/529280783
    /*
    *   1. org.springframework.beans.BeanUtils.copyProperties
    *   2. org.apache.commons.beanutils.BeanUtils.copyProperties
    *   3. object.clone() - 需要实现Cloneable 和重写clone()方法
    *   4. Cglib BeanCopier进行属性拷贝
    *
    * */
    @Test
    public void testSpringBeanUtilCopy() {
        StudentEntity source = new StudentEntity("Jack", 22, new Address("HeNan", 1));
        StudentEntity clone = new StudentEntity();
        org.springframework.beans.BeanUtils.copyProperties(source, clone);
        //org.apache.commons.beanutils.BeanUtils.copyProperties(clone, source);
        log.info("(clone == source) = {}", (clone == source));  // false 两个对象是不同的
        log.info("(clone == source) = {}", (clone.getAddress() == source.getAddress()));  // true 引用是一样的
        log.info("(clone == source) = {}", (clone.getName() == source.getName()));  // true 引用是一样的
        log.info("(clone == source) = {}", (clone.getAge() == source.getAge()));  // true 引用是一样的
        print(source, clone);

        System.out.println("set name = JJJJJJ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        source.setName("JJJJJJ");
        print(source, clone);
        log.info("(clone == source) = {}", (clone.getName() == source.getName()));  // false 字符串是final修饰

        System.out.println("set address = ShangHai ~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        source.getAddress().setAddress("ShangHai");
        log.info("(clone == source) = {}", (clone.getAddress() == source.getAddress()));  // true 同一个引用
        print(source, clone);
    }

    private void print(StudentEntity source, StudentEntity target) {
        log.info("source: {}", source);
        log.info("clone : {}", target);
    }

    public static void copy(Class source, Class target) {
        BeanCopier.create(source.getClass(), target.getClass(), false).copy(source, target, null);
    }

}

package com.ityj.tech.copy;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class StudentEntity {
    private String name;
    private Integer age;
    private Address address;


    @Override
    public String toString() {
        return "User{" +
                "name=" + name +
                ", age='" + age + '\'' +
                ", address=" + address +
                ", hashCode(): =" + this.hashCode() +
                '}';
    }

}

package com.ityj.tech.copy;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Address {
    private String address;
    private int id;}



二、深拷贝(Deep Copy)

序列化与反序列化

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值