DEV Community

Gowtham Kalyan
Gowtham Kalyan

Posted on

What is Serialization and Deserialization in Java?

Serialization is one of the most important concepts in Core Java, especially when working with file storage, caching, networking, distributed systems, and microservices.

It is also a very common Java interview question.

Let’s understand it clearly.


πŸ”Ή What is Serialization?

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

  • Saved to a file
  • Sent over a network
  • Stored in a database
  • Cached in memory

πŸ‘‰ In simple words:
Serialization = Converting object β†’ bytes


πŸ”Ή Why Do We Need Serialization?

In real-time applications:

  • Data must be transferred between systems
  • Objects need to be persisted
  • Sessions need to be stored
  • Distributed systems need object communication

Serialization makes this possible.


πŸ”Ή How to Make a Class Serializable?

To serialize an object, the class must implement the marker interface:

java.io.Serializable
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Example of Serialization

import java.io.*;

class Employee implements Serializable {
    int id;
    String name;

    Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

public class Test {
    public static void main(String[] args) throws Exception {
        Employee emp = new Employee(101, "Harish");

        FileOutputStream fos = new FileOutputStream("emp.ser");
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        oos.writeObject(emp);
        oos.close();

        System.out.println("Object Serialized");
    }
}
Enter fullscreen mode Exit fullscreen mode

This saves the object into a file named emp.ser.


πŸ”Ή What is Deserialization?

Deserialization is the reverse process of serialization.

It converts the byte stream back into an object.

πŸ‘‰ Deserialization = bytes β†’ object


πŸ”Ή Example of Deserialization

import java.io.*;

public class Test {
    public static void main(String[] args) throws Exception {

        FileInputStream fis = new FileInputStream("emp.ser");
        ObjectInputStream ois = new ObjectInputStream(fis);

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

        System.out.println(emp.id + " " + emp.name);
    }
}
Enter fullscreen mode Exit fullscreen mode

This reconstructs the object from the file.


πŸ”₯ Important Concepts in Serialization

βœ… 1. serialVersionUID

Used for version control of serialized classes.

private static final long serialVersionUID = 1L;
Enter fullscreen mode Exit fullscreen mode

If the class structure changes and serialVersionUID doesn’t match, Java throws:

InvalidClassException
Enter fullscreen mode Exit fullscreen mode

βœ… 2. transient Keyword

Variables marked as transient are not serialized.

transient String password;
Enter fullscreen mode Exit fullscreen mode

Used for sensitive data like passwords, OTP, session tokens.


βœ… 3. Static Variables

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


πŸ”Ή Real-Time Use Cases

Serialization is used in:

βœ” Caching (Redis, Hazelcast)
βœ” HTTP Sessions
βœ” RMI (Remote Method Invocation)
βœ” Microservices communication
βœ” Saving object state to file
βœ” Distributed systems


πŸ”Ή Interview Questions on Serialization

Interviewers often ask:

  • What happens if superclass is not Serializable?
  • Can we serialize static variables?
  • What is Externalizable?
  • What is serialVersionUID?
  • Difference between Serializable and Externalizable?
  • What is transient?

Understanding serialization deeply is very important for backend and enterprise Java roles.


🎯 Key Difference Summary

Feature Serialization Deserialization
Definition Object β†’ Byte Stream Byte Stream β†’ Object
Used For Storing / Sending Object Reconstructing Object
Stream Used ObjectOutputStream ObjectInputStream

πŸš€ Learn Serialization with Real-Time Projects

Serialization is widely used in enterprise applications, distributed systems, and backend development.

If you want practical implementation with real-world scenarios, check out:

πŸ”₯ AI powered Java Real Time Projects Online Training in Hyderabad

In this training, you will:

βœ” Work on real-time enterprise applications
βœ” Learn advanced Core Java concepts
βœ” Understand Serialization, Multithreading, JVM, Design Patterns
βœ” Build Spring Boot & Microservices projects
βœ” Gain AI-integrated backend development skills
βœ” Prepare confidently for interviews

Strong fundamentals + real-time project experience = Career growth πŸš€

Top comments (0)