Java - Serialization and Deserialization in Java

Serialization and deserialization are important mechanisms in Java used for converting objects into a format that can be easily stored or transmitted and then reconstructed back into an object when needed.

What is Serialization?

Serialization is the process of converting a Java object into a byte stream. This byte stream represents the object’s state, including its data members, in a form that can be:

  • Saved to a file

  • Sent over a network

  • Stored in a database or cache

In simple terms, serialization means “flattening” an object so it can be easily stored or transferred.

Java provides built-in support for serialization through the java.io.Serializable interface. It is a marker interface, meaning it does not contain any methods. When a class implements this interface, its objects become eligible for serialization.

Example conceptually:
If a class Student has fields like name, age, and marks, serialization converts this entire object into a sequence of bytes.

How Serialization Works

Java uses the ObjectOutputStream class to perform serialization. The object is written using a method like writeObject().

During this process:

  • The state of the object is captured

  • Primitive data types are converted directly

  • Object references are also serialized recursively (if they implement Serializable)

However, certain fields can be excluded using the transient keyword. Transient fields are not included in serialization.

What is Deserialization?

Deserialization is the reverse process of serialization. It converts the byte stream back into a live Java object.

This is done using the ObjectInputStream class with the method readObject().

In simple terms:

  • Serialization converts object → byte stream

  • Deserialization converts byte stream → object

Why Serialization is Important

Serialization is widely used in real-world Java applications because it enables data persistence and communication.

Key use cases include:

  1. Saving object state to files for later use

  2. Sending objects across networks in distributed systems

  3. Caching objects to improve performance

  4. Working with remote method invocation (RMI)

  5. Session storage in web applications

Role of serialVersionUID

Java uses a special identifier called serialVersionUID to ensure compatibility during deserialization.

If a class changes after an object is serialized, this ID helps verify whether the loaded object is compatible with the current class definition.

If it does not match, Java throws an InvalidClassException.

Important Points to Remember

  • Only objects of classes that implement Serializable can be serialized

  • Static variables are not serialized because they belong to the class, not the object

  • Transient variables are skipped during serialization

  • Serialization can increase storage size due to byte conversion

  • It is commonly used in distributed systems and Java EE applications

Simple Flow Summary

  1. Object is created in Java program

  2. Object is converted into byte stream (serialization)

  3. Byte stream is stored or transmitted

  4. Byte stream is read back

  5. Object is reconstructed (deserialization)

If you want, I can also provide a simple Java program example or real-world scenario to make it even easier to understand.