Getting Started with Java basics-shallow cloning and deep cloning of objects

Source: Internet
Author: User

This involves two classes, one for the person class and one for the test class


The first thing we're talking about is shallow cloning, which is completely unknown to an object when it's being cloned, it's simply a copy of domain-to-domain, and if it's a basic data type (INT,FLOAT,CHAR, etc.) to nothing, it's basically like a string, There is no problem with immutable objects such as Integer, but if you encounter a mutable object of date, or a mutable object of your own definition, he simply copies references to these mutable objects, rather than copying them again.


First on the person class, although this is the implementation of the cloneable interface, but there is no way to rewrite it, just super a bit to forget


Package Com.ray.object;import java.util.date;/** * person *  * @author Ray * @since 2015-05-07 * @version 1.0 *  */public Class Person implements cloneable {private int id = 0;private String name = "";p rivate Date birthday = null;public int ge TId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Date Getbirthday () {return birthday;} public void Setbirthday (Date birthday) {this.birthday = birthday;} @Overrideprotected Object Clone () {Object obj = null;try {obj = Super.clone ();} catch (Clonenotsupportedexception e) {//T ODO auto-generated catch Blocke.printstacktrace ();} return obj;}}


Then the test class

Package Com.ray.object;import java.util.date;/** * Shallow clone and deep clone * * @author Ray * @since 2015-05-07 * @version 1.0 * */public Class Test {public static void main (string[] args) {person bill = new person (); Bill.setid (1); Bill.setname ("Bill"); bill.se Tbirthday (New Date ()); System.out.println ("Bill.getid ()---" +bill.getid ()); System.out.println ("Bill.getname ()---" +bill.getname ()); System.out.println ("Bill.getbirthday ()---" +bill.getbirthday ()); Person Jack = (person) bill.clone (); System.out.println ("Jack.getid ()---" +jack.getid ()); System.out.println ("Jack.getname ()---" +jack.getname ()); System.out.println ("Jack.getbirthday ()---" +jack.getbirthday ()); try {thread.sleep (2000);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Date jacksbirthday = Jack.getbirthday (); Jacksbirthday.settime (System.currenttimemillis ()); System.out.println ("Bill.getid ()---" +bill.getid ()); System.out.println ("Bill.getname ()---" +bill.getname ()); System.out.println ("Bill.getbirthdaY ()---"+bill.getbirthday ()); System.out.println ("Jack.getid ()---" +jack.getid ()); System.out.println ("Jack.getname ()---" +jack.getname ()); System.out.println ("Jack.getbirthday ()---" +jack.getbirthday ());}}


Output:

Bill.getid ()---1
Bill.getname ()---Bill
Bill.getbirthday ()---Thu May 08:56:33 CST 2015
Jack.getid ()---1
Jack.getname ()---Bill
Jack.getbirthday ()---Thu May 08:56:33 CST 2015
Bill.getid ()---1
Bill.getname ()---Bill
Bill.getbirthday ()---Thu May 08:56:35 CST 2015
Jack.getid ()---1
Jack.getname ()---Bill
Jack.getbirthday ()---Thu May 08:56:35 CST 2015




As can be seen above, basic Jack This object is clone bill this object, but this is just a simple copy of the reference, when we modify the Jack object inside the birthday, Bill's birthday has changed



Now, let's talk about deep cloning, which is to copy all the fields of an object


First the person class, this time the person class overrides the Clone method, in addition to super, but also the inside of the birthday attribute also copied a copy


Package Com.ray.object;import java.util.date;/** * person *  * @author Ray * @since 2015-05-07 * @version 1.0 *  */public Class Person implements cloneable {private int id = 0;private String name = "";p rivate Date birthday = null;public int ge TId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public Date Getbirthday () {return birthday;} public void Setbirthday (Date birthday) {this.birthday = birthday;} @Overrideprotected Object Clone () {Object obj = null;try {obj = Super.clone ();                        Here's a copy of the birthday, too. Person person = (person) Obj;person.birthday = (Date) birthday.clone ();} catch (Clonenotsupportedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} return obj;}}

Again on the test class, in fact, the test class did not make any changes, here just for the convenience of Reading, also written on the


Package Com.ray.object;import java.util.date;/** * Shallow clone and deep clone * * @author Ray * @since 2015-05-07 * @version 1.0 * */public Class Test {public static void main (string[] args) {person bill = new person (); Bill.setid (1); Bill.setname ("Bill"); bill.se Tbirthday (New Date ()); System.out.println ("Bill.getid ()---" +bill.getid ()); System.out.println ("Bill.getname ()---" +bill.getname ()); System.out.println ("Bill.getbirthday ()---" +bill.getbirthday ()); Person Jack = (person) bill.clone (); System.out.println ("Jack.getid ()---" +jack.getid ()); System.out.println ("Jack.getname ()---" +jack.getname ()); System.out.println ("Jack.getbirthday ()---" +jack.getbirthday ()); try {thread.sleep (2000);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Date jacksbirthday = Jack.getbirthday (); Jacksbirthday.settime (System.currenttimemillis ()); System.out.println ("Bill.getid ()---" +bill.getid ()); System.out.println ("Bill.getname ()---" +bill.getname ()); System.out.println ("Bill.getbirthdaY ()---"+bill.getbirthday ()); System.out.println ("Jack.getid ()---" +jack.getid ()); System.out.println ("Jack.getname ()---" +jack.getname ()); System.out.println ("Jack.getbirthday ()---" +jack.getbirthday ());}}



Output:

Bill.getid ()---1
Bill.getname ()---Bill
Bill.getbirthday ()---Thu May 09:22:03 CST 2015
Jack.getid ()---1
Jack.getname ()---Bill
Jack.getbirthday ()---Thu May 09:22:03 CST 2015
Bill.getid ()---1
Bill.getname ()---Bill
Bill.getbirthday ()---Thu May 09:22:03 CST 2015
Jack.getid ()---1
Jack.getname ()---Bill
Jack.getbirthday ()---Thu May 09:22:05 CST 2015




As you can see from the above output, Jack's birthday changes no longer affect bill's birthday.



Finally, let's say that using = To copy the object, actually just copy the object's reference to another object, the object is not made any changes

Getting Started with Java basics-shallow cloning and deep cloning of objects

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.