In Java, serialization is the process of converting an object into a byte stream, so it can be saved to a file or transmitted over a network. Deserialization is the reverse process of converting the byte stream back into a copy of the original object.

Steps for Serialization and Deserialization

  1. Make the class Serializable:
    • The class whose objects you want to serialize must implement the Serializable interface. This interface is a marker interface, meaning it doesn’t contain any methods but signals to the JVM that the object can be serialized.
  2. Serialization:
    • Use ObjectOutputStream to write an object to an output stream (like a file).
  3. Deserialization:
    • Use ObjectInputStream to read the object from the input stream (like a file) and convert it back to the original object.

Example

import java.io.*;

// Employee class implements Serializable interface
class Employee implements Serializable {
    private String name;
    private transient int salary;  // Transient field will not be serialized
    
    public Employee(String name, int salary) {
        this.name = name;
        this.salary = salary;
    }
    
    public String getName() {
        return name;
    }
    
    public int getSalary() {
        return salary;
    }
}

public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // Create an Employee object
        Employee emp = new Employee("John Doe", 50000);
        
        // Serialization: Write the object to a file
        FileOutputStream fileOut = new FileOutputStream("employee.ser");
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        out.writeObject(emp);
        out.close();
        fileOut.close();
        System.out.println("Serialized data is saved in employee.ser");
        
        // Deserialization: Read the object from the file
        FileInputStream fileIn = new FileInputStream("employee.ser");
        ObjectInputStream in = new ObjectInputStream(fileIn);
        Employee deserializedEmp = (Employee) in.readObject();
        in.close();
        fileIn.close();
        
        // Print deserialized object
        System.out.println("Name: " + deserializedEmp.getName());  // Output: John Doe
        System.out.println("Salary: " + deserializedEmp.getSalary());  // Output: 0 (default value for int)
    }
}

Key Points

  1. Serializable Interface: Classes that can be serialized must implement the Serializable interface.
  2. Serialization:
    • Use ObjectOutputStream to serialize objects.
    • You can save the serialized object to files or send it over the network.
  3. Deserialization:
    • Use ObjectInputStream to deserialize objects.
    • The object is read back and restored to its original state (except for transient fields, which are set to default values).

Conclusion

Serialization and deserialization are essential in Java for storing and transmitting objects. Implementing Serializable and using ObjectOutputStream/ObjectInputStream allow you to easily serialize and deserialize objects to/from files or streams.