In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the meaning of deep copy and shallow copy in Java". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the meaning of deep copy and shallow copy in Java".
I. Preface
The word copy must be familiar to everyone, and it is often necessary to copy a document as a copy at work. The benefits of copying are also obvious, and it can save a lot of work compared to new ones. In Java, the concept of copy also exists, and the meaning of copy is that it can save the overhead of creating objects.
There is a method clone () in the Object class, which is as follows:
Protected native Object clone () throws CloneNotSupportedException
This method is modified by protected, and all classes in java inherit the Object class by default. The overloaded clone () method needs to be changed to public in order to ensure that other classes can be called normally.
This method is a native method, and the method modified by native is actually implemented by non-Java code, which is more efficient than the ordinary java method.
The return value of this method is an Object object, so we need to force it to the type we need.
This method throws a CloneNotSupportedException exception, which means that copying is not supported and requires us to implement the Cloneable interface to mark it. This class supports copying.
For the convenience of demonstration, we create two new entity classes, Dept and User, in which User depends on Dept. The entity class code is as follows:
Dept class:
@ Data @ AllArgsConstructor @ NoArgsConstructor public class Dept {private int deptNo; private String name;}
User class:
@ Data @ AllArgsConstructor @ NoArgsConstructor public class User {private int age; private String name; private Dept dept;}
2. Shallow copy
For properties of the basic type, the shallow copy copies the property values to the new object, while for the properties of the reference type, the shallow copy copies the reference to the new object. Reference types like String,Integer are immutable, and when copied, a new memory space is created to store the value, and the new reference points to the new memory space. Immutable types are special reference types, so let's assume that these reference types marked by final are also copied values.
Implementation of shallow copy function
@ Data @ AllArgsConstructor @ NoArgsConstructor public class User implements Cloneable {private int age; private String name; private Dept dept; @ Override protected Object clone () throws CloneNotSupportedException {return super.clone ()}}
How to verify our conclusion? First of all, compare whether the copied object is equal to the original object, which means it is a newly copied object. Secondly, modify the basic type property of the copied object. If this property of the original object is modified, the attribute of the basic type is the same. Finally, the reference type object of the copied object is modified, that is, the Dept property. If this property of the original object is changed, the property of the reference type is the same. After knowing the principle of the test, we write a piece of test code to verify our conclusion.
Public static void main (String [] args) throws Exception {Dept dept = new Dept (12, "Marketing Department"); User user = new User (18, "Java Journey", dept); User user1 = (User) user.clone (); System.out.println (user = = user1); System.out.println (); user1.setAge (20); System.out.println (user); System.out.println (user1); System.out.println () Dept.setName ("R & D Department"); System.out.println (user); System.out.println (user1);}
The running result of the above code is as follows:
False User {age=18, name='Java', dept=Dept {deptNo=12, name=' Marketing Department'} User {age=20, name='Java', dept=Dept {deptNo=12, name=' Marketing Department'} User {age=18, name='Java', dept=Dept {deptNo=12, name=' R & D'} User {age=20, name='Java', dept=Dept {deptNo=12, name=' R & D'}
III. Deep copy
Compared with a shallow copy, a deep copy replicates not only the properties of the basic type, but also the properties of the reference type.
Implementation of deep copy function
When copying the user, copy the dept attribute in the user at the same time.
Dept class:
@ Data @ AllArgsConstructor @ NoArgsConstructor public class Dept implements Cloneable {private int deptNo; private String name; @ Override public Object clone () throws CloneNotSupportedException {return super.clone ()}}
User class:
@ Data @ AllArgsConstructor @ NoArgsConstructor public class User implements Cloneable {private int age; private String name; private Dept dept; @ Override protected Object clone () throws CloneNotSupportedException {User user = (User) super.clone (); user.dept = (Dept) dept.clone (); return user;}}
Continue the test with the shallow copy of the test code, and the result is as follows:
False User {age=18, name='Java Journey, dept=Dept {deptNo=12, name=' Marketing'} User {age=20, name='Java Marketing', dept=Dept {deptNo=12, name=' Marketing'} User {age=18, name='Java Travel, dept=Dept {deptNo=12, name=' R & D'} User {age=20, name='Java Travel, dept=Dept {deptNo=12, name=' Marketing'}
In addition, you can use deserialization to achieve deep copy, first serializing the object into a byte stream, and then serializing the byte stream into an object, resulting in a new object.
Thank you for your reading, the above is "what is the meaning of deep copy and shallow copy in Java", after the study of this article, I believe you have a deeper understanding of the meaning of deep copy and shallow copy in Java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.