Home >Java >javaTutorial >Serialization and Deserialization in Java

Serialization and Deserialization in Java

王林
王林Original
2023-06-08 08:01:091835browse

Serialization and Deserialization in Java

Serialization refers to the process of converting the state of an object into a stream of bytes so that it can be transmitted over the network or permanently saved to disk. Deserialization is the process of restoring a serialized byte stream into an object. In Java, serialization and deserialization are implemented through ObjectInputStream and ObjectOutputStream.

Why do we need serialization?

In modern applications, the state of objects often needs to be transferred between different systems, processes, and threads. For example, in a distributed system, objects may need to be transferred between different servers. Another example is when storing objects in cache, the objects need to be serialized so that they can be saved to disk.

Serialization in Java

Java provides a Serializable interface to implement serialization. The Serializable interface is just a marker interface without any methods, it just tells the compiler that this class is serializable. If a class implements the Serializable interface, objects of that class can be serialized. For example:

public class Person implements Serializable {
  private static final long serialVersionUID = 1L;
  private String name;
  private int age;

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

  public String getName() {
    return name;
  }

  public int getAge() {
    return age;
  }
}

In the above example, the Person class implements the Serializable interface, indicating that the class can be serialized.

Deserialization in Java

Deserialization in Java is the process of restoring a serialized byte stream into an object. Deserialization can be implemented using the ObjectInputStream class. For example:

try {
  FileInputStream fileIn = new FileInputStream("person.ser");
  ObjectInputStream in = new ObjectInputStream(fileIn);
  Person person = (Person) in.readObject();
  in.close();
  fileIn.close();
} catch (IOException i) {
  i.printStackTrace();
} catch (ClassNotFoundException c) {
  c.printStackTrace();
}

In the above example, we first create a FileInputStream object and then pass it to the ObjectInputStream constructor to create the ObjectInputStream object. After that, we read the object from the ObjectInputStream using the readObject() method. Finally, we close the ObjectInputStream and FileInputStream.

It should be noted that when you deserialize, the serialized class must exist. If the serialized class does not exist, a ClassNotFoundException exception will be thrown.

Serialization and deserialization in Java are very important, allowing objects to be passed between different systems, processes and threads. At the same time, Java also provides some methods to control the serialization and deserialization behavior of objects, such as transient, writeObject(), and readObject() methods. Because serialization and deserialization have a large performance impact, their use should be avoided where appropriate.

The above is the detailed content of Serialization and Deserialization 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