search
HomeJavajavaTutorialJava Improvement Part 5 -----Using Serialization to Copy Objects

We know that this interface Cloneable exists in Java. Classes that implement this interface will have the ability to be copied. At the same time, copying is performed in memory. In terms of performance, it is faster than directly generating objects through new, especially in In the generation of large objects, the performance improvement is very obvious. However, we know that copy is divided into deep copy and shallow copy, but shallow copy has the problem of incomplete copy of object attributes. Regarding deep copy and shallow copy, please refer here: Gradual analysis of shallow copy and deep copy of java

1. Shallow copy problem

Let’s first look at the following code:

public class Person implements Cloneable{  
    /** 姓名 **/  
    private String name;  
      
    /** 电子邮件 **/  
    private Email email;  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public Email getEmail() {  
        return email;  
    }  
  
    public void setEmail(Email email) {  
        this.email = email;  
    }  
      
    public Person(String name,Email email){  
        this.name  = name;  
        this.email = email;  
    }  
      
    public Person(String name){  
        this.name = name;  
    }  
  
    protected Person clone() {  
        Person person = null;  
        try {  
            person = (Person) super.clone();  
        } catch (CloneNotSupportedException e) {  
            e.printStackTrace();  
        }  
          
        return person;  
    }  
}  
  
public class Client {  
    public static void main(String[] args) {  
        //写封邮件  
        Email email = new Email("请参加会议","请与今天12:30到二会议室参加会议...");  
          
        Person person1 =  new Person("张三",email);  
          
        Person person2 =  person1.clone();  
        person2.setName("李四");  
        Person person3 =  person1.clone();  
        person3.setName("王五");  
          
        System.out.println(person1.getName() + "的邮件内容是:" + person1.getEmail().getContent());  
        System.out.println(person2.getName() + "的邮件内容是:" + person2.getEmail().getContent());  
        System.out.println(person3.getName() + "的邮件内容是:" + person3.getEmail().getContent());  
    }  
}  
--------------------  
Output:  
张三的邮件内容是:请与今天12:30到二会议室参加会议...  
李四的邮件内容是:请与今天12:30到二会议室参加会议...  
王五的邮件内容是:请与今天12:30到二会议室参加会议...

In this application, first define an email, and then send the email to Zhang San, Li Si, and Wang Wu. Since they use the same email and only have different names, they use Zhang San’s object class Just copy the John Four and King Five objects and change the names. There is nothing wrong with the program until here, but if we need Zhang San to arrive 30 minutes in advance, we will modify the content of the email:

public class Client {  
    public static void main(String[] args) {  
        //写封邮件  
        Email email = new Email("请参加会议","请与今天12:30到二会议室参加会议...");  
          
        Person person1 =  new Person("张三",email);  
          
        Person person2 =  person1.clone();  
        person2.setName("李四");  
        Person person3 =  person1.clone();  
        person3.setName("王五");  
          
        person1.getEmail().setContent("请与今天12:00到二会议室参加会议...");  
          
        System.out.println(person1.getName() + "的邮件内容是:" + person1.getEmail().getContent());  
        System.out.println(person2.getName() + "的邮件内容是:" + person2.getEmail().getContent());  
        System.out.println(person3.getName() + "的邮件内容是:" + person3.getEmail().getContent());  
    }  
}

Here we also use the object of Zhang San to realize the processing of Li Si and Wang Wu Copy, and finally change the content of Zhang San's email to: Please come to the second conference room at 12:00 today to attend the meeting... But the result is:

张三的邮件内容是:请与今天12:00到二会议室参加会议...  
李四的邮件内容是:请与今天12:00到二会议室参加会议...  
王五的邮件内容是:请与今天12:00到二会议室参加会议...

Here we wonder why the content of Li Si and Wang Wu's emails was also changed? There will be objections if they are asked to arrive 30 minutes early!

In fact, the key to the problem lies in the clone() method. We know that the clone() method uses the clone() method of the Object class, but this method has a flaw. It does not copy the object's All attributes are copied, but copied selectively. The basic rules are as follows:

1. Basic type

If the variable is a basic type, copy its value, such as int, float wait.

2. Object

If the variable is an instance object, copy its address reference, which means that the new object and the original object share the instance variable at this time.

3. String string

If the variable is a String string, copy its address reference. But when modified, it will regenerate a new string from the string pool, and the original Ziducheng object remains unchanged.

Based on the above rules, we can easily find the problem. The three of them share a common object. If Zhang San modifies the content of the email, Li Si and Wang Wu will also modify it, so the above appears. Case. We can still solve this situation. We only need to create a new object in the clone() method, and then Zhang San references the object:

protected Person clone() {  
        Person person = null;  
        try {  
            person = (Person) super.clone();  
            person.setEmail(new Email(person.getEmail().getObject(),person.getEmail().getContent()));  
        } catch (CloneNotSupportedException e) {  
            e.printStackTrace();  
        }  
          
        return person;  
    }

So: Shallow copy is just a simple method provided by Java. The copy mechanism is not easy to use directly.

There is still a problem with the above solution. If there are a large number of objects in our system that are generated by copying, if we write a clone() method for each class, deep copies will also be required. , create a large number of new objects, this project is very large, here we can use serialization to copy the objects.

2. Use serialization to copy objects

How to use serialization to copy objects? It is relatively easy to achieve by copying the byte stream in memory. Write the parent object into a byte stream, and then read it out from the byte stream, so that you can create a new object, and there is no problem of reference sharing between the new object and the parent object. Really implement deep copy of objects.

public class CloneUtils {  
    @SuppressWarnings("unchecked")  
    public static <T extends Serializable> T clone(T obj){  
        T cloneObj = null;  
        try {  
            //写入字节流  
            ByteArrayOutputStream out = new ByteArrayOutputStream();  
            ObjectOutputStream obs = new ObjectOutputStream(out);  
            obs.writeObject(obj);  
            obs.close();  
              
            //分配内存,写入原始对象,生成新对象  
            ByteArrayInputStream ios = new ByteArrayInputStream(out.toByteArray());  
            ObjectInputStream ois = new ObjectInputStream(ios);  
            //返回生成的新对象  
            cloneObj = (T) ois.readObject();  
            ois.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return cloneObj;  
    }  
}

Objects using this tool class must implement the Serializable interface, otherwise there is no way to achieve cloning.

public class Person implements Serializable{  
    private static final long serialVersionUID = 2631590509760908280L;  
  
    ..................  
    //去除clone()方法  
  
}  
  
public class Email implements Serializable{  
    private static final long serialVersionUID = 1267293988171991494L;  
      
    ....................  
}

Therefore, objects using this tool class can achieve object cloning as long as they implement the Serializable interface, and there is no need to inherit the Cloneable interface to implement the clone() method.

public class Client {  
    public static void main(String[] args) {  
        //写封邮件  
        Email email = new Email("请参加会议","请与今天12:30到二会议室参加会议...");  
          
        Person person1 =  new Person("张三",email);  
          
        Person person2 =  CloneUtils.clone(person1);  
        person2.setName("李四");  
        Person person3 =  CloneUtils.clone(person1);  
        person3.setName("王五");  
        person1.getEmail().setContent("请与今天12:00到二会议室参加会议...");  
          
        System.out.println(person1.getName() + "的邮件内容是:" + person1.getEmail().getContent());  
        System.out.println(person2.getName() + "的邮件内容是:" + person2.getEmail().getContent());  
        System.out.println(person3.getName() + "的邮件内容是:" + person3.getEmail().getContent());  
    }  
}  
-------------------  
Output:  
张三的邮件内容是:请与今天12:00到二会议室参加会议...  
李四的邮件内容是:请与今天12:30到二会议室参加会议...  
王五的邮件内容是:请与今天12:30到二会议室参加会议...

The above is the content of Java Improvement Part 5-----Using Serialization to Copy Objects. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Java Platform Independence: Compatibility with different OSJava Platform Independence: Compatibility with different OSMay 13, 2025 am 12:11 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

What features make java still powerfulWhat features make java still powerfulMay 13, 2025 am 12:05 AM

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

Top Java Features: A Comprehensive Guide for DevelopersTop Java Features: A Comprehensive Guide for DevelopersMay 13, 2025 am 12:04 AM

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.

Is Java Truly Platform Independent? How 'Write Once, Run Anywhere' WorksIs Java Truly Platform Independent? How 'Write Once, Run Anywhere' WorksMay 13, 2025 am 12:03 AM

JavaisnotentirelyplatformindependentduetoJVMvariationsandnativecodeintegration,butitlargelyupholdsitsWORApromise.1)JavacompilestobytecoderunbytheJVM,allowingcross-platformexecution.2)However,eachplatformrequiresaspecificJVM,anddifferencesinJVMimpleme

Demystifying the JVM: Your Key to Understanding Java ExecutionDemystifying the JVM: Your Key to Understanding Java ExecutionMay 13, 2025 am 12:02 AM

TheJavaVirtualMachine(JVM)isanabstractcomputingmachinecrucialforJavaexecutionasitrunsJavabytecode,enablingthe"writeonce,runanywhere"capability.TheJVM'skeycomponentsinclude:1)ClassLoader,whichloads,links,andinitializesclasses;2)RuntimeDataAr

Is java still a good language based on new features?Is java still a good language based on new features?May 12, 2025 am 12:12 AM

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

What Makes Java Great? Key Features and BenefitsWhat Makes Java Great? Key Features and BenefitsMay 12, 2025 am 12:11 AM

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

Top 5 Java Features: Examples and ExplanationsTop 5 Java Features: Examples and ExplanationsMay 12, 2025 am 12:09 AM

The five major features of Java are polymorphism, Lambda expressions, StreamsAPI, generics and exception handling. 1. Polymorphism allows objects of different classes to be used as objects of common base classes. 2. Lambda expressions make the code more concise, especially suitable for handling collections and streams. 3.StreamsAPI efficiently processes large data sets and supports declarative operations. 4. Generics provide type safety and reusability, and type errors are caught during compilation. 5. Exception handling helps handle errors elegantly and write reliable software.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft