当前位置:网站首页>Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
Interview question: do you know the difference between deep copy and shallow copy? What is a reference copy?
2022-07-02 04:36:00 【J.King】
- Shallow copy : Shallow copy creates a new object on the heap , however , If the attribute inside the original object is a reference type , Shallow copy will directly copy the reference address of the internal object , That is, the copy object and the original object share the same internal object .
- Deep copy : Deep copy completely copies the entire object , Include the internal objects contained in this object
Icon :

Shallow copy
The sample code for a shallow copy is as follows , We have achieved Cloneable Interface , Rewrite the clone() Method .
clone() The implementation of this method is very simple , What is called directly is the parent class Object Of clone() Method .
public class Address implements Cloneable{
private String name;
// Omit constructor 、Getter&Setter Method
@Override
public Address clone() {
try {
return (Address) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
}
public class Person implements Cloneable {
private Address address;
private String name;
// Omit constructor 、Getter&Setter Method
@Override
public Person clone() {
try {
Person person = (Person) super.clone();
return person;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
}
test :
Person person1 = new Person(new Address(" Guangzhou "));
Person person1Copy = person1.clone();
// true
System.out.println(person1.getAddress() == person1Copy.getAddress());
As can be seen from the output structure , person1 Clone objects and person1 The same... Is still used Address object .
String The type is very special , First ,String Type is a reference data type , Not a basic data type , however String Data of type is stored in the constant pool , That is, it cannot be modified ! in other words , When person1 object person1.setName(" Xiaohong "); Revised name Type value , It's not changing the value of this data , It's a reference to this data from to Xiao Ming This constant points to Xiaohong This constant . under these circumstances , Another object person1Copy Of name Property values still point to Xiao Ming Not affected .
Person person1 = new Person(" Xiao Ming ");
Person person1Copy = person1.clone();
// Xiao Ming
System.out.println(person1.getName());
person1.setName(" Xiaohong ");
// Xiao Ming
System.out.println(person1Copy.getName());
Deep copy
Here we are simply right Person Class clone() Method to modify , With the intention of Person Inside the object Address Objects are copied together .
@Override
public Person clone() {
try {
Person person = (Person) super.clone();
person.setAddress(person.getAddress().clone());
return person;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
test :
Person person1 = new Person(new Address(" Guangzhou "));
Person person1Copy = person1.clone();
// false
System.out.println(person1.getAddress() == person1Copy.getAddress());
As can be seen from the output structure , although person1 Clone objects and person1 Contains Address The object is already different .
So what is a reference copy ? Simply speaking , Reference copy means that two different references point to the same object .
Object a = {
...}
Object b = a
Equal to multiple object names pointing to the same object
边栏推荐
- Handling of inconsistency between cursor and hinttext position in shutter textfield
- I sorted out some basic questions about opencv AI kit.
- oracle 存储过程与job任务设置
- Arbre binaire pour résoudre le problème (2)
- Typescript practice for SAP ui5
- Pytorch yolov5 exécute la résolution de bogues à partir de 0:
- [graduation season · advanced technology Er] young people have dreams, why are they afraid of hesitation
- geotrust ov多域名ssl证书一年两千一百元包含几个域名?
- A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
- Several methods of capturing packets under CS framework
猜你喜欢

Pytorch---使用Pytorch进行图像定位

Mysql表insert中文变?号的问题解决办法

How to write a client-side technical solution

How much can a job hopping increase? Today, I saw the ceiling of job hopping.

One step implementation of yolox helmet detection (combined with oak intelligent depth camera)

Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market

Realize the function of data uploading

Dare to go out for an interview without learning some distributed technology?

Yolov5 network modification tutorial (modify the backbone to efficientnet, mobilenet3, regnet, etc.)

C language practice - binary search (half search)
随机推荐
Exposure X8标准版图片后期滤镜PS、LR等软件的插件
cookie、session、tooken
Message mechanism -- message processing
One step implementation of yolox helmet detection (combined with oak intelligent depth camera)
缓存一致性解决方案——改数据时如何保证缓存和数据库中数据的一致性
Leetcode- insert and sort the linked list
C语言猜数字游戏
The solution to the complexity brought by lambda expression
Lei Jun wrote a blog when he was a programmer. It's awesome
Record the bug of unity 2020.3.31f1 once
FAQ | FAQ for building applications for large screen devices
Exposure X8 Standard Version picture post filter PS, LR and other software plug-ins
Bitmap principle code record
The confusion I encountered when learning stm32
Unit testing classic three questions: what, why, and how?
How muddy is the water in the medical beauty industry with a market scale of 100 billion?
Social media search engine optimization and its importance
How to solve the problem that objects cannot be deleted in Editor Mode
June book news | 9 new books are listed, with a strong lineup and eyes closed!
Common locks in MySQL