In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the difference between deep copy and shallow copy of Java". In daily operation, I believe that many people have doubts about the difference between deep copy and shallow copy of Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "what is the difference between deep copy and shallow copy of Java?" Next, please follow the editor to study!
How to distinguish between a deep copy and a shallow copy, in vernacular, is to assume that B replicates A, and when you modify A, see if B will change. If B also changes, it means that this is a shallow copy. If B does not change, it is a deep copy, self-reliant.
Let's first look at an example:
Let a = [0, 1, 2, 3, 4], b = a share console.log (a = = b); / / truea [0] = 9 share console.log (a, b); / / [9, 1, 2, 3, 4]
Yell ha, obviously b copied a, why modify the array a, array b also changed?
Basic data types and complex (reference) data types
Basic data types: number, string, boolean, null, undefined, symbol
Reference data type: Object class, unnecessary object {a: 1} with regular name-value pairs, array [1, 2, 3], functions, etc.
These two types of data are stored as follows:
1. Basic types-name values are stored in stack memory, such as let astat1
When you copy BPAA, the stack memory opens up a new memory, such as this:
So when you modify axi2 at this time, it will not affect b, because at this time b is on its own, its wings are hard, and it is no longer affected by a. Of course, let a copy 1, Benea; although b is not affected by a, this is not a deep copy, because the deep copy itself is only for the more complex object type data.
two。 Reference data type-the name exists in stack memory and the value exists in heap memory, but the stack memory provides a referenced address that points to the value in heap memory. Let's draw a diagram with the shallow copy above:
When brooma copies, it actually copies the reference address of a, not the value in the heap.
When we modify the array with a [0] = 1, because an and b point to the same address, naturally b is also affected, which is called shallow copy.
Well, if a new memory is opened up in heap memory to store values specifically for b, just like the basic type, it will achieve the effect of deep copy.
Implement a simple deep copy method?
Let's take a look at some simple examples.
1.slice () method
/ / first look at a method in js, the slice () method, which returns the selected element from an existing array. / / this method does not modify the array, but returns a subarray. ArrayObject.slice (start,end) let A1 = [1,2,3,4], b2 = a1.slice (); A1 [0] = 2; console.log (A1, b2)
As shown in the figure above, the slice () method seems to be a deep copy method! After modifying the above code snippet, let's take a look at it.
Let A1 = [1,2, [5Jing 6], 3,4], b2 = a1.slice (); A1 [0] = 2; A1 [2] [0] = 9; console.log (A1, b2)
The copy is not complete, and the first-level attribute of the b object is not affected, but the second-level attribute has not been copied successfully and still cannot be separated from the control of a, indicating that slice is not a real deep copy at all.
two。 Recursion
/ / A simple deep copy encapsulates the recursive function function deepClone (obj) {let objClone = Array.isArray (obj)? []: {} If (obj & & typeof obj = = "object") {for (key in obj) {if (obj.hasOwnProperty (key)) {/ / Note: the hasOwnProperty () method / / determines whether the obj child element is an object, if so Recursively copy console.log (obj [key]) if (obj [key] & & typeof obj [key] = "object") {objClone [key] = deepClone (obj [key]) } else {/ / if not, simply copy objClone [key] = obj [key];} return objClone;} let arr = [1,2, [5,6], 4], b1 = deepClone (arr); arr [2] [0] = 9; console.log (arr, b1) / / as previously imagined, b is now out of the control of an and is no longer affected by a. Once again, deep copy is the attribute of the copy object at all levels.
Note: the hasOwnProperty () official MDN explanation method returns a Boolean value indicating whether the object has a specified property in its own property (that is, whether there is a specified key).
3. In addition to recursion, we can borrow the parse and stringify of the JSON object.
Function deepClone (obj) {let _ obj = JSON.stringify (obj), objClone = JSON.parse (_ obj); return objClone} let a = [0L1, [2je 3], 4], b=deepClone (a); a [0] = 1th a [2] [0] = 1th console.log (ajol b)
4. In addition to the above two methods, we can also borrow JQ's extend method.
Extend ([deep], target, object1 [, objectN])
Deep indicates whether it is a deep copy. If it is true, it is a deep copy. If it is false, it is a shallow copy.
The target Object type target object to which the member properties of other objects are attached.
Object1 objectN is optional. The first and nth merged object of type Object.
Let a = [0jue 1, [2jue 3], 4], b=$.extend (true, [], a); a [0] = 1 a [2] [0] = 1 position console.log (afield b)
At this point, the study on "what is the difference between deep copy and shallow copy of Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.