Home >Java >javaTutorial >What is the process of object serialization in Java?

What is the process of object serialization in Java?

WBOY
WBOYOriginal
2024-04-12 11:30:02558browse

In Java, object serialization converts objects into byte streams, and deserialization restores byte streams into objects. Serialization requires implementing the Serializable interface, preparing the object and using ObjectOutputStream to write data; deserialization uses ObjectInputStream to read data and reconstruct the object. For example, the code serializes a Person object with name and age properties and deserializes it from a file to print the information.

What is the process of object serialization in Java?

The serialization process of objects in Java

Introduction

Serialization is a method of converting objects into byte streams and converting them into Procedures stored in files or on the network. Deserialization is the reverse process of converting a stored byte stream back into the original object. The core interface for object serialization in Java is Serializable.

Serialization process

1. Implement the Serializable interface: The class must implement the Serializable interface to be serialized.

2. Prepare the object: The object to be serialized must implement the writeObject method, which writes the object's fields to the output stream. If the object contains other serializable objects, the writeObject method needs to call the writeObject method of those objects as well.

3. Create ObjectOutputStream: Use ObjectOutputStream to write objects to the output stream.

4. Write the object: Call the writeObject method to write the object to the output stream.

Deserialization process

1. Create ObjectInputStream: Use ObjectInputStream to read objects from the input stream.

2. Read the object: Call the readObject method to read the object from the input stream. If the object contains other serializable objects, the readObject method also calls the readObject methods of those objects.

3. Reconstruct the object: After reading all the data from the input stream, the reflection mechanism will be used to reconstruct the object.

Practical Case

The following code example demonstrates how to serialize and deserialize objects in Java:

import java.io.*;

public class Person implements Serializable {
    private String name;
    private int age;

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

    @Override
    public void writeObject(ObjectOutputStream out) throws IOException {
        out.writeObject(name);
        out.writeInt(age);
    }

    @Override
    public void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        name = (String) in.readObject();
        age = in.readInt();
    }

    public static void main(String[] args) {
        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("person.ser"))) {
            Person person = new Person("John", 30);
            out.writeObject(person);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("person.ser"))) {
            Person person = (Person) in.readObject();
            System.out.println(person.name + ", " + person.age);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

The above is the detailed content of What is the process of object serialization in Java?. For more information, please follow other related articles on the PHP Chinese website!

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