← Back to Questions
Java

What is deserialization in Java?

Learn What is deserialization in Java? with simple explanations, real-time examples, interview tips and practical use cases.

Deserialization in Java is the process of converting a byte stream back into the original Java object.

In simple words:

Deserialization restores serialized objects from files, databases, or network streams back into memory.


Why Deserialization is Important?

Deserialization is used for:

  • Reading objects from files
  • Receiving objects over network
  • Restoring cached objects
  • Session recovery
  • Distributed systems communication
  • Microservices messaging

Deserialization Overview Diagram


Byte Stream

      |
      v

ObjectInputStream

      |
      v

Deserialization Process

      |
      v

Original Java Object Restored


Serialization vs Deserialization Flow


Java Object
      |
      v

Serialization

      |
      v

Byte Stream

      |
      v

Deserialization

      |
      v

Java Object Restored


Which Interface is Required?

The class must implement:

Serializable

Why?

Only Serializable objects can participate in serialization and deserialization.


Basic Serializable Class Example

import java.io.*;

class Employee
implements Serializable {

    int id;
    String name;

}

Serialization Example

FileOutputStream fos =
    new FileOutputStream(
        "employee.ser"
    );

ObjectOutputStream oos =
    new ObjectOutputStream(fos);

Employee emp =
    new Employee();

oos.writeObject(emp);

Deserialization Example

FileInputStream fis =
    new FileInputStream(
        "employee.ser"
    );

ObjectInputStream ois =
    new ObjectInputStream(fis);

Employee emp =
    (Employee) ois.readObject();

What Happens Internally?

  • Byte stream is read
  • ObjectInputStream reconstructs object
  • Original object state restored
  • Object becomes usable again

Deserialization Internal Flow


Serialized File

      |
      v

FileInputStream Reads Bytes

      |
      v

ObjectInputStream Converts Bytes

      |
      v

Object Restored in Heap Memory


Where is Object Restored?

Deserialized objects are restored inside:

Heap Memory

Memory Representation


Serialized Bytes

      |
      v

Heap Memory

Employee Object Restored


Does Constructor Execute During Deserialization?

For Serializable classes:

Constructors are NOT executed during deserialization.


Why?

Object is reconstructed directly from byte stream.


Important Interview Point

Constructors execute only during normal object creation using:

new

What Happens to transient Variables?

transient variables are NOT restored during deserialization.


Example

class User
implements Serializable {

    String username;

    transient String password;

}

After Deserialization

password = null

transient Flow


Serialization

      |
      +-------> username Saved

      |
      +-------> password Ignored

      |
      v

Deserialization

      |
      +-------> username Restored

      |
      +-------> password Missing


What is serialVersionUID?

serialVersionUID is a unique version identifier for serialized classes.


Example

private static final long
serialVersionUID = 1L;

Why Important During Deserialization?

It verifies compatibility between serialized object and current class definition.


Problem Without serialVersionUID

Class modifications may break deserialization.


Error Example

java.io.InvalidClassException

InvalidClassException Flow


Serialized Object Created

      |
      v

Class Modified Later

      |
      v

Deserialization Attempted

      |
      v

Version Mismatch

      |
      v

InvalidClassException


Deserialization with Inheritance

Child class objects can also be deserialized.


Example

class Person
implements Serializable {

}

class Employee
extends Person {

}

What if Parent is Not Serializable?

Parent default constructor executes during deserialization.


Difference Between Serialization and Deserialization

Feature Serialization Deserialization
Purpose Object → Byte Stream Byte Stream → Object
Stream Used ObjectOutputStream ObjectInputStream
Operation Write Read
Output Bytes Object

Deserialization Security Risks

Improper deserialization can create serious security vulnerabilities.


Security Risks

  • Remote code execution
  • Malicious object injection
  • Arbitrary code execution
  • Data tampering

Unsafe Deserialization Flow


Untrusted Byte Stream

      |
      v

Application Deserializes Data

      |
      v

Malicious Object Executed

      |
      v

Security Breach


Security Best Practices

  • Never deserialize untrusted data
  • Validate incoming objects
  • Use allow-listing
  • Prefer JSON/XML for APIs
  • Use secure serialization libraries

Deserialization in Banking Systems

Banking systems use deserialization for:

  • Transaction recovery
  • Distributed caching
  • Audit restoration
  • Session replication

Example Scenario


Serialized Transaction Data

      |
      v

Banking Server Reads Data

      |
      v

Transaction Object Restored


Deserialization in E-Commerce Systems

E-commerce platforms use deserialization for:

  • Shopping cart restoration
  • Session recovery
  • Cache retrieval
  • Distributed order processing

Deserialization in Spring Boot

Spring Boot applications use deserialization for:

  • REST request conversion
  • JSON to DTO mapping
  • Redis cache retrieval
  • Kafka consumer processing

Spring Boot Example

@PostMapping("/users")
public void save(
    @RequestBody User user
) {

}

What Happens?

JSON request body is deserialized into Java object.


REST Deserialization Flow


Incoming JSON

      |
      v

Jackson Library

      |
      v

Java DTO Object Created


Deserialization in Microservices

Microservices architectures heavily use deserialization for:

  • Kafka event processing
  • REST API communication
  • RabbitMQ messaging
  • Distributed system integration

Microservice Communication Flow


Kafka Message Received

      |
      v

JSON Deserialized

      |
      v

Java Event Object Created

      |
      v

Business Logic Executed


Java Deserialization vs JSON Deserialization

Feature Java Deserialization JSON Deserialization
Input Binary Data JSON Text
Readable No Yes
Used In Java Systems REST APIs
Common Library ObjectInputStream Jackson/Gson

Advantages of Deserialization

  • Restores objects efficiently
  • Supports distributed systems
  • Useful for caching
  • Enables session persistence

Disadvantages of Deserialization

  • Security risks
  • Version compatibility problems
  • Performance overhead
  • Binary data difficult to debug

Common Interview Mistake

Many developers think constructors execute during deserialization.

Actually:

  • Constructors are NOT executed for Serializable classes.

Another Common Mistake

Many developers think transient variables are restored after deserialization.

Actually:

  • transient variables are ignored during serialization.

Best Practices

  • Always define serialVersionUID
  • Avoid deserializing untrusted data
  • Use transient for sensitive fields
  • Prefer JSON for APIs
  • Validate deserialized objects carefully

Realtime Enterprise Example

Kafka Consumer Processing


Order Event Received

      |
      v

JSON Deserialized

      |
      v

Java Order Object Created

      |
      v

Inventory Updated


Related Learning Topics


Professional Interview Answer

Deserialization in Java is the process of converting a byte stream back into the original Java object using ObjectInputStream. It restores object state from files, network streams, distributed caches, or serialized storage. Only classes implementing Serializable can participate in deserialization. Important concepts include transient variables, serialVersionUID, object compatibility, and secure deserialization practices. Deserialization is widely used in enterprise applications, Spring Boot systems, banking platforms, Kafka messaging, Redis caching, cloud-native microservices, distributed systems, and REST API processing where objects must be reconstructed from stored or transmitted data.


Frequently Asked Questions

What is deserialization in Java?

Deserialization converts byte stream back into Java object.

Which class is used for deserialization?

ObjectInputStream is used for deserialization.

Does constructor execute during deserialization?

No, constructors are not executed for Serializable classes.

Why is serialVersionUID important?

It ensures compatibility between serialized objects and class versions.

Are transient variables restored after deserialization?

No, transient variables are not serialized or restored.

Why this Java question is important?

This interview question helps candidates understand real-time backend development concepts, practical problem solving, coding fundamentals, system design basics and production-ready application behavior.

Practice this question carefully for Java backend roles, Spring Boot developer interviews, microservices interviews, company interviews and full-stack developer preparation.

About the Author

Naresh Kumar is a Senior Java Backend Engineer with experience building enterprise applications using Java, Spring Boot, Microservices, Docker, Kubernetes and Cloud technologies.