当前位置:网站首页>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
边栏推荐
- Exposure X8 Standard Version picture post filter PS, LR and other software plug-ins
- powershell_ View PowerShell function source code (environment variable / alias) / take function as parameter
- [JS -- map string]
- Is it safe to open an account with first venture securities? I like to open an account. How can I open it?
- IDEA xml中sql没提示,且方言设置没用。
- Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
- Several methods of capturing packets under CS framework
- Pytorch-Yolov5從0運行Bug解决:
- Geotrust OV Multi - Domain Domain SSL Certificate rmb2100 per year contains several Domain names?
- BGP experiment the next day
猜你喜欢
Deep understanding of lambda expressions
阿里云polkit pkexec 本地提权漏洞
cs架构下抓包的几种方法
Starting from the classification of database, I understand the map database
云服务器的安全设置常识
Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
Thinkphp內核工單系統源碼商業開源版 多用戶+多客服+短信+郵件通知
How much is the tuition fee of SCM training class? How long is the study time?
Landing guide for "prohibit using select * as query field list"
随机推荐
云服务器的安全设置常识
Common locks in MySQL
Li Kou interview question 02.08 Loop detection
I sorted out some basic questions about opencv AI kit.
What is 5g industrial wireless gateway? What functions can 5g industrial wireless gateway achieve?
Typescript practice for SAP ui5
How much is the tuition fee of SCM training class? How long is the study time?
[C language] basic learning notes
Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
Learn what definitelytyped is through the typescript development environment of SAP ui5
Wechat applet calculates the distance between the two places
Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial
Realize the function of data uploading
Pytorch-Yolov5从0运行Bug解决:
CY7C68013A之keil编译代码
Idea autoguide package and autodelete package Settings
Vmware安装win10报错:operating system not found
There is no prompt for SQL in idea XML, and the dialect setting is useless.
Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
The difference between vectorresize and reverse.