Self-understanding of the Clone method of object class

Source: Internet
Author: User

Online Search:

Clone () is the protected method of the Java.lang.Object class, implementing the Clone method:

1) The class itself needs to implement the Cloneable interface

2) to override the Clone () method, it is best to set the modifier modifier to public so that it can be called across packages.

Shallow copy and deep copy

1. Shallow copy and deep copy concept
⑴ Shallow copy (shallow clone)
All the 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, a shallow copy simply duplicates the object being considered, without

Copy the object that it refers to.

⑵ deep Copy (Deep clone)
All the variables of the copied object contain the same values as the original object, removing the variables that refer to other objects. Variables that refer to other objects will point to new objects that have been copied, not the original

Some of those objects that are referenced. In other words, a deep copy copies the objects that are referenced by the object being copied again.

2. The Clone () method of Java
The Clone method copies a copy of the object and returns it to the caller. Generally, the clone () method satisfies:
① to any object x, there are X.clone ()!=x//The cloned object is not the same object as the original object
② has X.clone () for any object X. getclass () = =x.getclass ()//Clone the object as the type of the original object
③ if the Equals () method of object x is defined properly, then X.clone (). Equals (x) should be established.

Shallow copy:

classA{String s= "A.1";}classCImplementscloneable{a A=NewA (); inti = 14; @OverrideprotectedC Clone ()throwsclonenotsupportedexception {returnCSuper. Clone (); }} Public classTest_clone { Public Static voidMain (string[] args) {c C=NewC (); C.A.S= "A.2"; C C_c=NULL; Try{C_c=C.clone (); } Catch(clonenotsupportedexception e) {e.printstacktrace ();        } System.out.println (c);        System.out.println (C_c);        System.out.println (C.A);    System.out.println (C_C.A); }}

Deep copy:

classAImplements cloneable{String s= "A.1"; @Override protected Object Clone () throws Clonenotsupportedexception {return Super . Clone (); }}classCImplementscloneable{a A=NewA (); inti = 14; @OverrideprotectedC Clone ()throwsclonenotsupportedexception { c c = (C) super.clone (); C.A = (a) a.clone ();//Clone A ()        returnC; }} Public classTest_clone { Public Static voidMain (string[] args) {c C=NewC (); C.A.S= "A.2"; C C_c=NULL; Try{C_c=C.clone (); } Catch(clonenotsupportedexception e) {e.printstacktrace ();        } System.out.println (c);        System.out.println (C_c);        System.out.println (C.A);    System.out.println (C_C.A); }}

The Clone () method of object is to replicate everything from the original object to the newly opened heap memory. The base type in the cloned object, and references to reference types will be copied. So a deep copy is a reference type within the cloned object, then clone () once, opening up a new heap of memory to hold the object of the reference type.

Self-understanding of the Clone method of object class

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.