Java Serialization In-Depth Guide and Applications
In Java, serialization refers to the process of converting a Java object into a byte stream, enabling its storage in files or transmission over networks. This mechanism allows Java objects to communicate across different Java Virtual Machines (JVMs) and ensures that objects retain their state after program restarts.
Serialization in Java is achieved by implementing the Serializable interface. If a class implements the Serializable interface, its instances can be serialized and deserialized. Serialization converts Java objects into byte streams using the ObjectOutputStream class, while deserialization uses the ObjectInputStream class to convert byte streams back into Java objects.
It is important to note that serialization only captures the state of an object, not the code of the class itself. Therefore, when deserializing, it is crucial to ensure that the class code already exists; otherwise, a ClassNotFoundException will be thrown.
Java also offers advanced features for serialization, such as customizing the process by implementing the Externalizable interface and marking fields with the transient keyword to exclude them from serialization. Additionally, Java provides a mechanism known as Java Serialization XML, which allows Java objects to be serialized into XML format data.
Serialization Examples
Here is a simple example of serializing and deserializing a Java object:
import java.io.*;
public class SerializationExample {
public static void main(String[] args) {
// Create a Person object
Person person = new Person("张三", 20);
// Serialize the Person object to a file
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("person.ser"))) {
oos.writeObject(person);
System.out.println("Person object has been serialized to the file");
} catch (IOException e) {
e.printStackTrace();
}
// Deserialize the Person object from the file
try (ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("person.ser"))) {
Person deserializedPerson = (Person) ois.readObject();
System.out.println("Deserialized Person object is: " + deserializedPerson);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
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;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
In this example, we define a class named Person that implements the Serializable interface. We then create a Person object, serialize it to a file, and subsequently deserialize it from the file, printing its contents.
It is important to note that we defined a serialVersionUID field in the Person class; this is the version number used by the Java serialization mechanism to determine whether serialized objects and deserialized objects are compatible. If the serialVersionUID of the serialized object does not match that of the deserialized object, deserialization will fail. Therefore, it is standard practice to explicitly declare serialVersionUID when defining a serializable class.