Java's clone implementation

Source: Internet
Author: User

After the

understands all the details behind implementing the Clone () method, you can create a class that can be easily replicated to provide a local copy:
 

: Localcopy.java//creating local copies with Clone () import java.util.*;
  Class MyObject implements cloneable {int i;
  MyObject (int ii) {i = II}
    public Object Clone () {Object o = null;
    try {o = Super.clone ();
    catch (Clonenotsupportedexception e) {System.out.println ("MyObject can ' t clone");
  return o;
  Public String toString () {return integer.tostring (i); } public class Localcopy {static MyObject g (MyObject v) {//Passing a handle, modifies outside object:v.i+
    +;
  return v;
    Static MyObject F (MyObject v) {v = (MyObject) v.clone ();//local copy v.i++;
  return v;
    public static void Main (string[] args) {MyObject a = new MyObject (11);
    MyObject B = g (a);
    Testing handle equivalence,//Not object equivalence:if (a = = b) System.out.println ("A = = B");
    Else System.out.println ("a!= B");
    System.out.println ("a =" + a); System.out.println ("b =" + b);
    MyObject C = new MyObject (47);
    MyObject d = f (c);
    if (c = = d) System.out.println ("c = = d");
    else System.out.println ("C!= D");
    System.out.println ("c =" + C);
  System.out.println ("D =" + D); }
} ///:~

Anyway, clone () must be accessible, so it must be set to public. Second, as the initial action of clone (), you should call the base class version of Clone (). The Clone () that is invoked here is predefined within object. It is invoked because it has a protected (protected) property, so it can be accessed in a derived class.
Object.clone () checks how large the original object is, and then frees up enough memory for the new object to copy all the bits from the original object to the new object. This is called "bitwise duplication" and, as a general idea, the job should be done by the clone () method. But before Object.clone () formally begins, it checks whether a class is cloneable, that is, whether it has cloning capabilities--in other words, whether it implements the Cloneable interface. If not, Object.clone () throws a clonenotsupportedexception violation, stating that we cannot clone it. Therefore, it is best to surround (or encapsulate) the calling code of the Super.clone () with a try-catch block to try to catch a violation that should never occur (since the Cloneable interface is indeed implemented here).
in Localcopy, two methods G () and F () reveal differences between the two parameter passing methods. where G () demonstrates passing by reference, it modifies the external object and returns a reference to that external object. The F () is a clone of the independent variable, so isolate it and keep the original object separate. Subsequently, it continued to do what it wished. You can even return a handle to the new object and do not have any side effects on the original object. Note the following somewhat odd statement:
V = (MyObject) v.clone ();
It does so by creating a local copy. To avoid being confused by such a statement, remember that this rather odd form of encoding is entirely permissible in Java, because everything that has a name is actually a handle. So handle V is used to clone a copy that it points to, and eventually returns a handle to the underlying type object (as it is defined in Object.clone ()) and must then be styled to the correct type.
in Main (), the difference between two different parameters is that they test a different method separately. The output results are as follows:
 

A = = b
a =
B =
C!= d
c =
D = 48

Keep in mind the fact that Java tests for "equivalence" do not check the interior of the objects being compared to verify that their values are the same. the = = and!= operators Simply contrast the contents of the handle. If the address within the handle is the same, the handle is assumed to point to the same object, so they are considered "equivalent". So the operator really detects "because of an alias problem, does the handle point to the same object?" ”

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.