A comprehensive analysis of the Java object Clone method _java

Source: Internet
Author: User
Tags shallow copy

1 protected native Object clone () throws Clonenotsupportedexception;

1. The method is modified by native keyword

The native keyword in Java indicates that this method is a local method, "Java native description." Moreover, the native modified method performs more efficiently than native .

2. The method is modified by protected

When a class overrides the Clone () method, it needs to be modified to the public access modifier, so that all other classes can access this method of the class.

3. Method throws clonenotsupportedexception exception

A class wants to overwrite the Clone () method and must implement the java.lang.Cloneable interface itself, otherwise it throws a clonenotsupportedexception exception.

Second, the role of Clone ()

Note: Our objects here refer specifically to complex types.

1, simple = operation

We know that objects of complex types in Java are reference types, and they tend to have memory addresses for objects. So we can't simply assign operations by using the = operator. We assign an object A to another object B, and we just assign the memory address of object A to B so that both objects are pointing to the same memory address. As a result, modifications to one of the objects will affect the other object. The following figure indicates:

person P1 = new person ();
person P2 = p1;

2, Clone ()

Using the Clone () method, you can quickly create a copy of an object, and two objects point to a different memory address. The following figure indicates:

person P1 = new person ();
person P2 = P1.clone ();

Iii. Shallow clone and deep clone1, shallow clone (shallow copy)

Shallow clone refers to only the clone object itself, not the fields in the Clone object. Only call Super.clone (), just shallow clone. Although the object after the copy points to a different memory address, the field inside the object still points to the same memory address as the previous object.

public class Shallowclone implements cloneable {public

  String name;
  public int age;
  public by person;

  Public Shallowclone () {
  } public

  Shallowclone (String name, int age, person person) {
    this.name = name;
    This.age = age;
    This.person = person;
  }

  @Override Public
  Shallowclone Clone () {
    shallowclone c = null;
    try {
      c = (shallowclone) super.clone ();
      return c;
    } catch (Clonenotsupportedexception e) {
      e.printstacktrace ();
    }
    return c;
  }

  public static void Main (string[] args) {person
    p = new Person ();
    P.name = "P";
    P.age = ten;

    Shallowclone C1 = new Shallowclone ("Jim", p);
    System.out.printf ("Before clone:c1 =%s, C1.person =%s\n", C1, C1.person);
    Shallowclone C2 = C1.clone ();
    System.out.printf ("After clone:c2 =%s, C2.person =%s\n", c2, C2.person);
  }

Run main () output:

Before clone:c1 = cre.sample.test.object.shallowclone@558385e3, C1.person = Cre.sample.test.person@2dcb25f1
After clone:c2 = cre.sample.test.object.shallowclone@742808b3, C2.person = cre.sample.test.person@2dcb25f1

Description shallow copy, Shallowclone object memory address changed, but the person field memory address in the object has not changed;

2, Deep clone (deep copy)

Deep clone refers to the fields within the Clone object itself, as well as the clones.

/** * Deep Clone code example * Created by Cregu on 2016/6/9.
  */public class Deepclone implements cloneable {public String name;
  public int age;

  public by person;
    Public Deepclone () {} public Deepclone (String name, int age, person person) {this.name = name;
    This.age = age;
  This.person = person;
    @Override public Deepclone Clone () {Deepclone c = null;
      try {c = (Deepclone) super.clone ();
      C.person = Person.clone ();
    return C;
    catch (Clonenotsupportedexception e) {e.printstacktrace ();
  return C;
    public static void Main (string[] args) {person p = new person ();
    P.name = "P";

    P.age = 10;
    Deepclone C1 = new Deepclone ("Jim", p);
    System.out.printf ("Before clone:c1 =%s, C1.person =%s\n", C1, C1.person);
    Deepclone C2 = C1.clone ();
  System.out.printf ("After clone:c2 =%s, C2.person =%s\n", c2, C2.person); }
}

Run main () output:

Before clone:c1 = cre.sample.test.object.deepclone@558385e3, C1.person = cre.sample.test.person@2dcb25f1 after
CLONE:C2 = cre.sample.test.object.deepclone@742808b3, C2.person = cre.sample.test.person@70535b58

Description deep copy, Deepclone object memory address changed, but the person field memory address in the object also changed.

The above text of the Java Object Clone method comprehensive analysis is small to share all the content of everyone, hope to give you a reference, but also hope that we support cloud habitat community.

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.