Backend Development 6 min read

Understanding Java Serialization and Deserialization

This article explains what Java serialization and deserialization are, why they are needed for object persistence and distributed systems, and provides step‑by‑step code examples showing how to implement them using the Serializable interface and ObjectOutputStream.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding Java Serialization and Deserialization

Click the "Java Interview Questions精选" link above to follow the public account.

Interview practice and gap filling.

What are Java serialization and deserialization?

Java serialization is the process of converting a Java object into a byte sequence, while deserialization restores the byte sequence back into a Java object.

Serialization: The main purpose is to preserve object integrity and transportability when transmitting or storing objects. It converts an object into an ordered byte stream for network transmission or local file storage, enabling state saving and reconstruction.

Deserialization: A client obtains the serialized byte stream from a file or network and rebuilds the original object using the stored state and metadata.

Why do we need serialization and deserialization?

Serialization offers several advantages:

1. Distributed objects: Enables remote method invocation (RMI) to run services on remote hosts as if they were local.

2. Deep copy of object graphs: Recursively saves the data of referenced objects, allowing full object hierarchy to be written to a byte stream and later reconstructed.

3. Persisting classes: Classes can be written to files or databases; later they can be read back and re‑instantiated, preserving all variables and state.

4. Uniform data format: After serialization, everything becomes a byte stream, making it easy to transmit or store different kinds of objects, files, or data in a common format.

How to implement Java serialization and deserialization

First, make the class to be serialized implement the Serializable interface.

For example, to serialize the name and age fields of a Person class:

import java.io.Serializable;

public class Person implements Serializable { // this class can be serialized
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String toString() {
        return "姓名:" + this.name + ",年龄" + this.age;
    }
}

Then serialize the object (convert the two fields into binary data):

package org.lxh.SerDemo;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

public class ObjectOutputStreamDemo { // serialization
    public static void main(String[] args) throws Exception {
        // Create the output file
        File file = new File("D:" + File.separator + "person.ser");
        ObjectOutputStream oos = null;
        // Decorator stream
        oos = new ObjectOutputStream(new FileOutputStream(file));
        // Instantiate the class
        Person per = new Person("张三", 30);
        oos.writeObject(per); // serialize the object
        oos.close();
    }
}

This simple example demonstrates the basic steps for Java serialization and deserialization; for deeper understanding, consult additional resources.

References:

《Java对象的序列化(Serialization)和反序列化详解》 https://blog.csdn.net/yaomingyang/article/details/79321939
《Java 序列化的高级认识》 https://www.ibm.com/developerworks/cn/java/j-lo-serial/

Follow us for more interview questions and technical articles.

backendjavaserializationdeserializationObjectIO
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.