Effects of Java Object.clone ()

Source: Internet
Author: User
Tags bitwise shallow copy tostring

When the

calls Object.clone (), what actually happens? What is the key to Super.clone () when we cover clone () in our own class? The Clone () method in the root class is responsible for establishing the correct storage capacity and copying the bits from the original object to the storage space of the new object through "bitwise Replication". That is, it does not just reserve storage space and replicate an object-you actually need to investigate the exact size of the object you want to replicate, and then copy that object. Since all of this work is done in the internal code of the clone () method defined by the root class (the root class does not know what to inherit from here), you may have guessed that the process needs to use Rtti to determine the actual size of the object that you want to clone. In this way, the Clone () method establishes the correct amount of storage space and makes the correct bitwise replication of that type.
Regardless of what we do, the first part of the cloning process should usually be called super.clone (). By making an accurate copy, this will provide a good basis for the subsequent cloning process. You can then take some other necessary action to complete the final clone.
to understand exactly what other operations are, first of all, it's a good idea what object.clone () brings us. In particular, does it automatically clone the target that all the handles point to? The following example completes this form of instrumentation:
 

//: Snake.java//Tests cloning to-if destination to//handles are also cloned.
  public class Snake implements cloneable {private Snake next;
  private char C;
    Value of i = = number of segments Snake (int i, char x) {c = x;
  if (i > 0) next = new Snake (i, (char) (x + 1));
    } void Increment () {C + +;
  if (next!= null) next.increment ();
    public string toString () {string s = ":" + C;
    if (next!= null) s + = next.tostring ();
  return s;
    public Object Clone () {object o = null;
    try {o = Super.clone ();
  catch (Clonenotsupportedexception e) {} return o;
    public static void Main (string[] args) {Snake s = new Snake (5, ' a ');
    System.out.println ("s =" + s);
    Snake s2 = (Snake) s.clone ();
    System.out.println ("s2 =" + s2);
    S.increment ();
  System.out.println ("after s.increment, s2 =" + s2); }
} ///:~

A snake (snake) is composed of several paragraphs, each of which is of a snake type. So, this is a list of links to a section. All segments are created in a circular fashion, and each time it is done, the value of the first builder parameter is decremented until the end is zero. To give each paragraph a unique token, the value of the second parameter (a char) is incremented each time the Loop builder calls.
The effect of the increment () method is to cycle through each tag so that we can see the changes that occur, and ToString loops out each tag. The output is as follows:

s =: a:b:c:d:e
s2 =: a:b:c:d:e after
s.increment, s2 =: a:c:d:e:f

This means that only the first segment is replicated by Object.clone (), so this is a "shallow copy". If you want to replicate the entire snake-that is, "deep copy"-You must take additional action in the overridden clone ().
It is usually possible to invoke Super.clone () from a class that can be cloned to ensure that all underlying class actions (including Object.clone ()) are available. As is explicitly calling a clone () for each handle within the object, those handles are aliased to the handle of the original object. The constructor calls are also roughly the same--first constructing the underlying class, then the next derivative builder ... And so on until you are in the deepest derivative builder. The difference is that clone () is not a builder, so there is no way to implement automatic cloning. In order to clone, it must be explicitly carried out by itself.

Related Article

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.