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
πΉ 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");
}
}
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);
}
}
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;
If the class structure changes and serialVersionUID doesnβt match, Java throws:
InvalidClassException
β 2. transient Keyword
Variables marked as transient are not serialized.
transient String password;
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)