Interview Knowledge 2.5: copies of Java objects to improve articles - Object clone (copy)

61. Why use a clone?

62. How to achieve the object clone?

63. What is the deep and shallow copy copy difference is?

 

61. Why use a clone?

Like object to a process wanted to maintain the original data next operation, it is necessary cloned.

62. How to achieve the object clone?

Object class clone method is to copy an object, the object is assigned a separate independent, its own memory space.

When you create an object using the clone method, you must implement an interface: Cloneable, there is no way this interface is a marker interface. Since Object class, rights-modified clone method is protected, so when we clone an object, the object to be cloned to override the clone method. And the authority to modify the public, in other places can call this cloning method.

63. What is the deep and shallow copy copy difference is?

Shallow clone, clone the object when, only the basic data types of data is replicated in the past; if it is a reference type, only the reference copy in the past, that is, the object being cloned and the original object, a reference to a type of common property references.
Deep clone: when cloning, will basic data types and data types of data references, while copying. Clone object and the original object does not reference a common reference type
 drawback: in deep cloning, if the reference object relationships are more complicated, cumbersome cloning, because each object must be cloned.
   Solution: You can use serialization to resolve.

Java improving articles - Object clone (copy)

How to use the clone copy objects? Shallow clone and a deep clone

Java difference in protected rights and privileges by default

Guess you like

Origin www.cnblogs.com/zfyang2429/p/11131694.html