当前位置:网站首页>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
边栏推荐
- How muddy is the water in the medical beauty industry with a market scale of 100 billion?
- Actual combat | use composite material 3 in application
- Feature Engineering: summary of common feature transformation methods
- Lei Jun wrote a blog when he was a programmer. It's awesome
- Homework of the 16th week
- Sword finger offer II 006 Sort the sum of two numbers in the array
- Bitmap principle code record
- Vmware安装win10报错:operating system not found
- Wechat applet calculates the distance between the two places
- Unit testing classic three questions: what, why, and how?
猜你喜欢
Yyds dry inventory compiler and compiler tools
UNET deployment based on deepstream
Pytorch---使用Pytorch进行图像定位
The core idea of performance optimization, dry goods sharing
cs架构下抓包的几种方法
Mysql表insert中文变?号的问题解决办法
66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
FAQ | FAQ for building applications for large screen devices
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
win11安装pytorch-gpu遇到的坑
随机推荐
C language practice - binary search (half search)
66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
二叉樹解題(二)
Let genuine SMS pressure measurement open source code
Dare to go out for an interview without learning some distributed technology?
Pytorch---使用Pytorch实现U-Net进行语义分割
MySQL advanced SQL statement 2
千亿市场规模医疗美容行业的水究竟有多浑?
Why can't you remember when reading? Why can't you remember- My technology learning methodology
MySQL table insert Chinese change? Solution to the problem of No
Bitmap principle code record
Ten thousand volumes are known to all, and one page of a book is always relevant. TVP reading club will take you through the reading puzzle!
CY7C68013A之keil编译代码
【毕业季·进击的技术er】年少有梦,何惧彷徨
unable to execute xxx. SH: operation not permitted
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
Pytoch yolov5 runs bug solution from 0:
Read "the way to clean code" - function names should express their behavior
Okcc why is cloud call center better than traditional call center?