Java (30)-Object shallow clone and deep clone

Source: Internet
Author: User

I. The concept of shallow cloning and deep cloning:

1). Shallow clone: Also known as shallow copy, all 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, not the object it refers to.

2). Deep clone: Also known as deep copy, all variables of the copied object contain the same value as the original object, removing those variables that refer to other objects. Variables that refer to other objects will point to new objects that have been copied, not those that are already referenced. In other words, a deep copy is a copy of the object referenced by the object being copied.

Two. Shallow cloning implementation:

<pre class= "java" name= "code" ><strong><span style= "font-size:18px;"        >public class Main {public static void Main (string[] args) throws Exception {Student student1 = new Student ();        Student1.setname ("Zhang San");        Student1.setsex ("male");        Student1.setnumber (10); Student Student2 = (Student) student1.clone ();        System.out.println (Student2.getname () + "" + student2.getnumber () + "" + student2.getsex ()); Student1.setname ("John Doe"); System.out.println (Student1.getname ()); System.out.println (Student2.getname ()); System.out.println (Student1 = = Student2); System.out.println (student1.getclass () = = Student2.getclass ());}} Class Student implements Cloneable{int number; String name; String sex;public int GetNumber () {return number;} public void Setnumber (int number) {this.number = number;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;} @OverrideprotecteD Object Clone () throws clonenotsupportedexception {Object obj = Super.clone (); return obj;}} </span></strong>


Print:

Zhang 310 Male
John doe
Tom
False
True

The Clone method of object is shallow clone, Clone () method satisfies (1). For any object, obj, there is obj.clone ()!=obj, and the cloned object is not an object from the original object.


Three. Deep-clone implementations (this is a cumbersome approach, typically implemented with serialization):

public class Main {public static void main (string[] args) throws Exception {Teacher Teacher = new Teacher (); Teacher.setnam E ("John Doe"); Teacher.setnumber (20); Student stu = new Student (), Stu.setname ("Zhang San"); Stu.setnumber (Stu.setsex); ("male"); Stu.setteacher (teacher); Student STU2 = (Student) stu.clone (); System.out.println (Stu2.getteacher (). GetName ()) Teacher.setname ("Harry");//If it is a shallow clone print Harry, because the teacher reference does not change, It becomes time for the Name property in teacher System.out.println (Stu2.getteacher (). GetName ());}} Class Teacher implements Cloneable{int number; String name;public int GetNumber () {return number;} public void Setnumber (int number) {this.number = number;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} @Overrideprotected Object Clone () throws Clonenotsupportedexception {return Super.clone ();}} Class Student implements Cloneable{int number; String name;    String sex;    Teacher Teacher; Public Teacher Getteacher () {return Teacher;} public void Setteacher (Teacher Teacher) {this.teacher = Teacher;} Public int GetNumber () {return number;} public void Setnumber (int number) {this.number = number;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;} @Overrideprotected Object Clone () throws Clonenotsupportedexception {Student Student = (Student) super.clone (); Studen T.setteacher ((Teacher) Student.getteacher (). Clone ()); return student;}}

Print:

John doe
John doe




Java (30)-Object shallow clone and deep clone

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.