Java Serialization and Deserialization

📘 Java 👁 79 views 📅 Dec 01, 2025
⏱ Estimated reading time: 2 min

Serialization and deserialization in Java are mechanisms to convert objects into a stream of bytes and reconstruct them back into objects, enabling persistent storage or network transfer of object states.


1. What is Serialization?

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

  • Saved to a file

  • Sent over a network

  • Stored in a database

Key Points:

  • Only objects of classes that implement the Serializable interface can be serialized.

  • Transient variables (transient) are not serialized.


2. What is Deserialization?

Deserialization is the reverse process of serialization, where the byte stream is converted back into a copy of the original object.


3. Serializable Interface

  • A marker interface (contains no methods).

  • Used to indicate that a class can be serialized.

import java.io.Serializable; class Student implements Serializable { private String name; private int age; Student(String name, int age) { this.name = name; this.age = age; } public String toString() { return "Name: " + name + ", Age: " + age; } }

4. Serialization Example

import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.io.IOException; public class SerializeExample { public static void main(String[] args) { Student s = new Student("John", 21); try { FileOutputStream file = new FileOutputStream("student.ser"); ObjectOutputStream out = new ObjectOutputStream(file); out.writeObject(s); // Serialize object out.close(); file.close(); System.out.println("Object serialized successfully"); } catch (IOException e) { e.printStackTrace(); } } }

5. Deserialization Example

import java.io.FileInputStream; import java.io.ObjectInputStream; import java.io.IOException; public class DeserializeExample { public static void main(String[] args) { try { FileInputStream file = new FileInputStream("student.ser"); ObjectInputStream in = new ObjectInputStream(file); Student s = (Student) in.readObject(); // Deserialize object in.close(); file.close(); System.out.println("Deserialized Student: " + s); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } }

6. Key Points

  • Serializable interface → Marker interface to enable serialization.

  • transient keyword → Prevents a variable from being serialized.

  • serialVersionUID → Ensures compatibility between serialized and deserialized objects.

  • Serialization allows persistent object storage and network communication.


7. Advantages

  • Persistence: Objects can be saved and restored.

  • Data Transfer: Objects can be sent over networks.

  • Caching: Serialized objects can be stored temporarily.

  • Ease of Use: Simple mechanism to store entire object state.


8. Conclusion

Serialization and deserialization in Java allow saving and restoring object states, making Java programs persistent, network-capable, and flexible. Proper use of Serializable and transient ensures data consistency and security during storage or transfer.


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes