Java Serialization and Deserialization – Complete Guide with Examples


Learn how to serialize Java objects into byte streams and deserialize them back, including implementation, transient fields, and versioning for efficient object storage and transfer.

Serialization and Deserialization in Java – Complete Detailed Tutorial

Serialization in Java allows you to convert objects into a byte stream for storage or transmission.

Deserialization is the reverse process – converting the byte stream back into a Java object.

These concepts are part of java.io package.

1. Why Serialization is Used

  1. Persist objects to files or databases
  2. Send objects over networks (RMI, sockets)
  3. Store objects in cache
  4. Clone objects or maintain object state

2. Serializable Interface

  1. Marker interface (no methods)
  2. Classes implementing Serializable can be serialized

import java.io.Serializable;

class Student implements Serializable {
private String name;
private int age;

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

@Override
public String toString() {
return name + " (" + age + ")";
}
}

Key Points:

  1. Implements java.io.Serializable
  2. No methods to override
  3. All fields are serialized by default

3. Serializing Objects

  1. Use ObjectOutputStream with FileOutputStream

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class Main {
public static void main(String[] args) {
Student student = new Student("Alice", 22);

try (FileOutputStream fos = new FileOutputStream("student.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos)) {

oos.writeObject(student); // serialize object
System.out.println("Student object serialized successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
}

Explanation:

  1. Object is converted into a byte stream
  2. Saved in student.ser file

4. Deserializing Objects

  1. Use ObjectInputStream with FileInputStream

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class Main {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("student.ser");
ObjectInputStream ois = new ObjectInputStream(fis)) {

Student student = (Student) ois.readObject(); // deserialize object
System.out.println("Deserialized Student: " + student);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}

Output:


Deserialized Student: Alice (22)

5. Transient Fields

  1. Fields declared as transient are not serialized

class Student implements Serializable {
private String name;
private transient int age; // will not be serialized

public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
  1. During deserialization, age will be default value (0)

6. serialVersionUID

  1. Unique identifier for class versioning
  2. Helps maintain compatibility during deserialization

private static final long serialVersionUID = 1L;
  1. If class structure changes, it prevents InvalidClassException

7. Key Points

  1. Serialization converts object → byte stream
  2. Deserialization converts byte stream → object
  3. Use transient for non-serializable fields
  4. Always define serialVersionUID for version control
  5. Implements Serializable interface (marker interface)

8. Summary

  1. Java provides ObjectOutputStream and ObjectInputStream
  2. Essential for persistent storage and network communication
  3. Works with custom objects safely
  4. Core concept for Java I/O Streams