Advanced Java October 06 ,2025

Serialization and Deserialization in Java

Complete Detailed Guide

Introduction

In Java, Serialization is the process of converting an object into a byte stream so it can be:

  • Stored in a file
  • Transmitted over a network
  • Persisted for later use

Deserialization is the reverse process — converting the byte stream back into a copy of the original object.

Serialization and deserialization are fundamental for:

  • Saving object states
  • Transferring objects between JVMs
  • Implementing caching and persistence
  • Enabling remote communication in distributed systems

This blog explains serialization and deserialization in detail, with examples, use cases, limitations, and best practices.

Step 1 — Understanding Serialization

Serialization allows an object’s state to be saved and recreated later. Java provides the Serializable interface to mark a class as serializable.

Key Points:

  • Serializable is a marker interface (no methods to implement).
  • Only objects of classes implementing Serializable can be serialized.
  • Static fields are not serialized (since they belong to the class, not the object instance).
  • Transient fields are skipped during serialization.

Example of Serialization

Person Class:

import java.io.Serializable;

public class Person implements Serializable {
    private static final long serialVersionUID = 1L;

    private String name;
    private int age;
    private transient String password; // will not be serialized

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

    @Override
    public String toString() {
        return "Person{name='" + name + "', age=" + age + ", password='" + password + "'}";
    }
}

Serialize Object:

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

public class SerializeDemo {
    public static void main(String[] args) {
        Person person = new Person("John", 30, "secret123");

        try (FileOutputStream fileOut = new FileOutputStream("person.ser");
             ObjectOutputStream out = new ObjectOutputStream(fileOut)) {

            out.writeObject(person);
            System.out.println("Object has been serialized: " + person);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output Example:

Object has been serialized: Person{name='John', age=30, password='secret123'}

This creates a file person.ser containing the serialized byte stream of the object.

Step 2 — Understanding Deserialization

Deserialization recreates the original object from a byte stream stored in a file or received over a network.

Deserialize Object:

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

public class DeserializeDemo {
    public static void main(String[] args) {
        try (FileInputStream fileIn = new FileInputStream("person.ser");
             ObjectInputStream in = new ObjectInputStream(fileIn)) {

            Person person = (Person) in.readObject();
            System.out.println("Object has been deserialized: " + person);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Expected Output:

Object has been deserialized: Person{name='John', age=30, password='null'}

Notice: The password field is null because it was marked transient.

Step 3 — serialVersionUID

serialVersionUID is a unique identifier for a serializable class.
It is used during deserialization to verify that the sender and receiver of a serialized object have compatible classes.

If serialVersionUID does not match, Java throws:

java.io.InvalidClassException

Example:

private static final long serialVersionUID = 1L;

Always define serialVersionUID to maintain version compatibility.

Step 4 — Customizing Serialization

Java provides two special methods for customization:

  • private void writeObject(ObjectOutputStream out)
  • private void readObject(ObjectInputStream in)

Example:

private void writeObject(ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();
    out.writeObject(password); // manually serialize transient field
}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    password = (String) in.readObject();
}

This allows:

  • Controlling how fields are serialized.
  • Encrypting sensitive data.
  • Implementing custom logic during serialization/deserialization.

Step 5 — Externalizable Interface

Java also provides the Externalizable interface for complete control over serialization.

Key points:

  • Requires implementing writeExternal(ObjectOutput out) and readExternal(ObjectInput in).
  • No automatic serialization of fields — everything must be explicitly handled.

Example:

import java.io.*;

public class PersonExternal implements Externalizable {
    private String name;
    private int age;

    public PersonExternal() {} // required

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

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeUTF(name);
        out.writeInt(age);
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException {
        name = in.readUTF();
        age = in.readInt();
    }

    @Override
    public String toString() {
        return "PersonExternal{name='" + name + "', age=" + age + "}";
    }
}

 Advantages of Serialization

  1. Object Persistence: Allows saving object state to files or databases.
  2. Communication: Enables transferring objects over networks in distributed applications.
  3. Caching: Useful for caching objects.
  4. Deep Copy: Serialization can create deep copies of objects.

Disadvantages of Serialization

  1. Performance Overhead: Serialization/deserialization consumes CPU and memory.
  2. Security Risks: Serialized objects can be tampered with.
  3. Versioning Issues: Changes to classes require careful handling with serialVersionUID.
  4. Transient Fields: Not serialized unless explicitly handled.

Best Practices

  • Always define serialVersionUID.
  • Use transient for sensitive data.
  • Use Externalizable for fine-grained control.
  • Avoid unnecessary serialization to improve performance.
  • Use custom serialization methods for security.

Real-World Use Cases

Serialization is widely used in:

  • Java RMI (Remote Method Invocation) for remote object calls.
  • Caching frameworks like Ehcache, Hazelcast.
  • Message Brokers like Kafka (object messages).
  • Persistence in file storage or databases.
  • Session storage in web applications.

Summary

Serialization and deserialization in Java are powerful features that allow objects to be stored, transferred, and reconstructed.
By implementing the Serializable interface, Java developers can make objects persistent and transferable between different parts of an application or across JVMs.
However, serialization comes with overhead, security risks, and potential version compatibility issues that must be managed carefully.

Customizing serialization and using Externalizable gives developers fine control over how objects are saved and restored, making serialization a versatile tool in Java’s toolbox for persistence, communication, and caching.

 

Sanjiv
0

You must logged in to post comments.

Related Blogs

Generics P...
Advanced Java August 08 ,2025

Generics Part- 2

Collection...
Advanced Java July 07 ,2025

Collections Framewor...

Mastering...
Advanced Java August 08 ,2025

Mastering Java Multi...

Annotation...
Advanced Java August 08 ,2025

Annotations

Java Multi...
Advanced Java August 08 ,2025

Java Multithreading...

Java Memor...
Advanced Java August 08 ,2025

Java Memory Manageme...

Java Lambd...
Advanced Java August 08 ,2025

Java Lambda Expressi...

Java Funct...
Advanced Java August 08 ,2025

Java Functional Inte...

Java Strea...
Advanced Java August 08 ,2025

Java Stream API

JDBC (Java...
Advanced Java August 08 ,2025

JDBC (Java Database...

JDBC (Java...
Advanced Java September 09 ,2025

JDBC (Java Database...

Annotation...
Advanced Java August 08 ,2025

Annotations

Generics
Advanced Java August 08 ,2025

Generics

Java I/O (...
Advanced Java August 08 ,2025

Java I/O (NIO)

Introducti...
Advanced Java September 09 ,2025

Introduction to Desi...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Other Prin...
Advanced Java September 09 ,2025

Other Principles Beh...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Builder De...
Advanced Java September 09 ,2025

Builder Design Patte...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Chain of R...
Advanced Java September 09 ,2025

Chain of Responsibil...

Command De...
Advanced Java September 09 ,2025

Command Design Patte...

Interprete...
Advanced Java September 09 ,2025

Interpreter Design P...

Iterator D...
Advanced Java September 09 ,2025

Iterator Design Patt...

Mediator D...
Advanced Java September 09 ,2025

Mediator Design Patt...

Memento De...
Advanced Java September 09 ,2025

Memento Design Patte...

Observer D...
Advanced Java September 09 ,2025

Observer Design Patt...

State Desi...
Advanced Java September 09 ,2025

State Design Pattern...

Strategy D...
Advanced Java September 09 ,2025

Strategy Design Patt...

Template M...
Advanced Java September 09 ,2025

Template Method Desi...

Visitor De...
Advanced Java September 09 ,2025

Visitor Design Patte...

Prototype...
Advanced Java September 09 ,2025

Prototype Design Pat...

Java 8+ Fe...
Advanced Java October 10 ,2025

Java 8+ Features

SOLID Prin...
Advanced Java October 10 ,2025

SOLID Principles in...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

How Iterat...
Advanced Java October 10 ,2025

How Iterators Work i...

How Concur...
Advanced Java October 10 ,2025

How ConcurrentHashMa...

Comparable...
Advanced Java October 10 ,2025

Comparable vs Compar...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Semaphore...
Advanced Java October 10 ,2025

Semaphore in Java

ExecutorSe...
Advanced Java October 10 ,2025

ExecutorService in J...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Producer-C...
Advanced Java October 10 ,2025

Producer-Consumer Pr...

Implementi...
Advanced Java October 10 ,2025

Implementing a Custo...

Busy Spin
Advanced Java October 10 ,2025

Busy Spin

Segment Lo...
Advanced Java October 10 ,2025

Segment Locking in J...

Tree Bins...
Advanced Java October 10 ,2025

Tree Bins in Java

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Get In Touch

G06, Kristal Olivine Bellandur near Bangalore Central Mall, Bangalore Karnataka, 560103

+91-8076082435

techiefreak87@gmail.com