Copy in Java (ii) deep clone

Source: Internet
Author: User

A shallow copy (The Clone () method in the object class) refers to a copy of a variable for the base data type when the object is copied, whereas a variable for a reference type simply copies the reference.

A deep copy (or Deep clone) is a copy of the object content associated with the object and the object.

Entity class:

Package Com.lin.test;import java.io.serializable;/** * @athor TianLin * * June 28, 2015 PM 1:56:18 * **/public class Dog Imple ments serializable{    private static final long serialversionuid = 1L;        Private String dogname;    Public String Getdogname () {        return dogname;    }    public void Setdogname (String dogname) {        this.dogname = Dogname;    }} Package Com.lin.test;import java.io.serializable;/** * @athor TianLin * * June 28, 2015 PM 1:43:24 * **/public class User Impl Ements serializable{        private static final long serialversionuid = 1L;    Private String username;        Private dog dog;    Public String GetUserName () {        return username;    }    public void Setusername (String username) {        this.username = username;    }    Public Dog Getdog () {        return dog;    }    public void Setdog (dog dog) {        This.dog = dog;    }    }

Clone Execution Class:

Package Com.lin.test;import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import Java.io.objectinputstream;import java.io.objectoutputstream;/** * @athor TianLin * * June 28, 2015 PM 1:44:08 * **/public CLA SS Objcloner {        @SuppressWarnings ("unchecked") public    static  <t>t cloneobj (t obj) {                t retVal = null;                 try{                        //Writes the object to the stream            bytearrayoutputstream BAOs = new Bytearrayoutputstream ();            ObjectOutputStream oos = new ObjectOutputStream (BAOs);            Oos.writeobject (obj);                                    Reads objects from the stream            bytearrayinputstream Bais = new Bytearrayinputstream (Baos.tobytearray ());            ObjectInputStream  ois = new ObjectInputStream (Bais);                        RetVal = (T) ois.readobject ();                    } catch (Exception e) {            e.printstacktrace ();        }                return retVal;}    }

Test class:

Package com.lin.test;/** * @athor TianLin * * June 28, 2015 PM 1:42:47 * **/public class Clonetest {public static void        Main (string[] args) {User user = new User ();                User.setusername ("Zhang San");        Dog dog = new Dog ();                Dog.setdogname ("Puppy 1");                User.setdog (dog);                User User2 = objcloner.cloneobj (user);        SYSTEM.OUT.PRINTLN ("User username:" +user.getusername ());        System.out.println ("User dogname:" + User.getdog (). Getdogname ());        System.out.println ("User2 username:" +user2.getusername ());                System.out.println ("User2 dogname:" + User2.getdog (). Getdogname ());                System.out.println ("-------------------------------------");        User2.setusername ("John Doe");                User2.getdog (). Setdogname ("Puppy 2");        SYSTEM.OUT.PRINTLN ("User username:" +user.getusername ());        System.out.println ("User dogname:" + User.getdog (). Getdogname ()); System.out.println ("UseR2 Username: "+user2.getusername ());    System.out.println ("User2 dogname:" + User2.getdog (). Getdogname ()); }}

Operation Result:

User username: Zhang San user dogname: Puppy 1user2 Username: Zhang San user2 dogname: Puppy 1-------------------------------------user Userna Me: Zhang San user dogname: Puppy 1user2 Username: John Doe user2 dogname: Puppy 2

Excerpt from: http://www.cnblogs.com/Jtianlin/p/4605477.html

Copy in Java (ii) 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.