Recently, I was reading objective Java, where I talked about cloning, so today I want to study cloning.
We all know that there are two better ways to copy a corresponding one, one is serialization, and the other is cloning. It is convenient to use serialization for replication, because this method will automatically perform deep replication, and we only need the
Java basic usage tips: Two Methods of deep copy
(1) shallow copy (shallow clone)
All variables of the Copied object contain the same value as the original object, and all references to other objects still point to the original object. In other words, the shortest copy only
Deep copy of clone () in Java
Show the shortest copy: for non-basic data types, after the clone, the two pointers point to the same memory space, so it is only a shortest copy, so if you operate on an object, the other content also changes, which is obviously unreasonable.
⑴ Shallow copy (shallow clone)
All the variables of the copied object contain the same values as the original object, and all references to other objects still point to the original object. In other words, a shallow copy replicates only the object being considered, not the object it references.
⑵ deep Copy (
essence or the key-value pair, the right design, very convenient.Conclusion:If you want to use JSON, Json-lib is a piece of crap, it's a lab work ... Use Jackson, please.2. Cloneable interface?I have always had a point of view that Java provides native API performance that is more efficient than any of its own.Unfortunately, cloneable interface first, there is no public object clone. I don't know what kind of plane he's doing. The inheritance interfa
, you think, deep copy is re- Then the reference value pointing to the new object is certainly not the same as the old one!The object serialization method is to refer to other articles, which are linked here.When the class implements the Serializable interface, its object is serializable. In fact, Serializable is an empty interface whose purpose is simply to identify an object of a class that can be seriali
Recently, we need to compare the changes of two object attributes, one of which is oldObj, the other is newObj, And the oldObj is the previous status of newObj. Therefore, when newObj is in a certain state, copy the same object. Because JAVA does not support deep copy, a method is specially written.
The method implemen
The project uses Ehcache as a buffer between the program and the database, the cache object is modified during use, such as Plan.setlangsymbol (), where the cache is used, and the data taken is modified, so the guess is caused by a shallow reference to the cache.In fact, StackOverflow also mentions this issueCache.get () returns deep copy of element? 》As for how to do d
follows:Java.io.NotSerializableException:com.example.test.TeacherIn the case where the serializable interface is implemented, the results are as follows:Student1:student{sno=1, name= ' Rye ', teacher=teacher{tno=2, name= ' Lintong '}}student2:student{sno=1, Name= ' Rye ', teacher=teacher{tno=1, name= ' Lintong 'falseView CodeThrough the way of object flow, the deep copy is successfully completed.Three. Ove
1. Use Randomaccessfile to achieve file compositing.2. Use serialization technology in Java to implement an object graph (100 cat cat collections, but cat owner does not need serialization) for deep replication.3. Describe the serialization of the process, application scenarios, the purpose of serial ID.===========================================================================1. Use Randomaccessfile to ach
A shallow copy (The Clone () method in the object class) refers to a copy of a variable for the base data type when the object is copied, whereas a variable for a reference type simply copies the reference.A deep copy (or Deep clone) is a
LeetCode138 Copy List with Random Pointer (deep replication of linked lists with Random pointers) Java question
Question:
A linked list is given such that each node contains an additional random pointer which cocould point to any node in the list or null.
Return a deep copy
Today, we suddenly used the most basic java value reference and value copy, as well as the object's shortest cloning and deep cloning. Don't go round. Let's look at the example directly.Simple Type value reference, which also belongs to the value struct;Int a = 3Int B =Then, no matter how a changes the value, the value of B will not change because of a, but the f
Recently, you need to compare two object properties, one of which is oldobj, and the other is that Newobj,oldobj is the previous state of newobj, so you need to copy one of the same objects in a state of newobj, because Java does not support deep copy, So specifically wrote a method
The implementation of the method is
The implementation of the method is simple and offers two ways:One is to serialize to a stream of data, provided that all objects (objects contained in the object ...) are serialized. You need to inherit the serializable interface, and if you inherit it, it's easy, if you don't inherit it, and you don't want to modify all the classes, you can use the second way.The second is to serialize the object into JSON, using JSON to implement the copy, which re
public object Deepcopy (object source) {Object destination = new Object ();Bytearrayoutputstream bo = null;ObjectOutputStream oo = null;Bytearrayinputstream bi = null;ObjectInputStream oi = null;try {Read from SourceBo = new Bytearrayoutputstream ();oo = new ObjectOutputStream (bo);Oo.writeobject (source);Write to Destinationbi = new Bytearrayinputstream (Bo.tobytearray ());Oi = new ObjectInputStream (BI);Destination = (Object) oi.readobject ();} catch (ClassNotFoundException e) {E.printstacktra
The following is a deep, shallow copy defined in C + +
When you initialize another newly constructed object with a custom class type Object that has already been initialized, the copy constructor is automatically called. In other words, the copy constructor is called when the object of the class needs to be Cop
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.