Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Serialization and Deserialization in I/O

1. Introduction

Serialization is the process of converting an object into a byte stream, while deserialization is the reverse process, converting a byte stream back into an object. These processes are crucial for saving the state of an object or transferring it over a network.

2. Key Concepts

  • Serialization: The transformation of an object into a format that can be easily stored or transmitted.
  • Deserialization: The reverse process of serialization.
  • Serializable Interface: A marker interface in Java to indicate that a class can be serialized.
  • ObjectOutputStream & ObjectInputStream: Classes used for serialization and deserialization respectively.

3. Serialization

In Java, a class must implement the Serializable interface to enable serialization.

Note: The fields of a class that should not be serialized can be marked as transient.

4. Deserialization

Deserialization is performed using the ObjectInputStream class to read the byte stream and reconstruct the object.

5. Code Example

Below is an example demonstrating serialization and deserialization:

import java.io.*;

class Person implements Serializable {
    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 + '}';
    }
}

public class SerializationDemo {
    public static void main(String[] args) {
        Person person = new Person("John Doe", 30);
        
        // Serialization
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.ser"))) {
            oos.writeObject(person);
            System.out.println("Serialized: " + person);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        // Deserialization
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.ser"))) {
            Person deserializedPerson = (Person) ois.readObject();
            System.out.println("Deserialized: " + deserializedPerson);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

6. Best Practices

  • Always declare the serialVersionUID for serializable classes.
  • Use transient for sensitive information that should not be serialized.
  • Consider using Externalizable for more control over serialization.
  • Keep your serialized objects as simple as possible.

7. FAQ

What is serialVersionUID?

It is a unique identifier for each class that implements Serializable. It helps during deserialization to verify that the sender and receiver of a serialized object have loaded classes that are compatible with respect to serialization.

Can I serialize any object in Java?

No, only objects of classes that implement the Serializable interface can be serialized.

What happens if I do not implement Serializable?

If you attempt to serialize an object of a class that does not implement Serializable, a java.io.NotSerializableException will be thrown.