Serialization And Deserialization In Java

Updated: Dec 26, 2019

Serialization and Deserialization is one of the oldest and important concepts in Java. This article is an easy step-by-step guide to understand everything about it.

Quick Introduction

Serialization in Java is the process of converting the state of an object into a byte stream. This enables to save the byte stream into a disk or to transfer them over the network.

Deserialization in Java is the opposite of serialization. It is the process of converting the serialized byte stream back into the actual object.

Serializable vs Externalizable

A java object is serializable if it implements the Serializable or Externalizable interface. Let’s dive deep into understanding the difference between the two interfaces.

Serializable Interface

  • Serializable interface extends the Externalizable interface but it is a marker interface. i.e., it does not contain any methods.

  • In case of the Serializable interface, a default serialization process takes care of serialization.

Externalizable Interface

  • Externalizable interface contains two methods. i.e., writeExternal() and readExternal().

  • In case of the Externalizable interface, there is no default serialization process. User has to specify the process by implementing its methods.

Serialization and Deserialization

Java provides ObjectInputStream and ObjectOutputStream classes to help perform serialization and deserialization.

  • The writeObject(Object obj) method of ObjectOutputStream class helps in serialization. It converts a serializable object into a byte stream.

  • The readObject() method of ObjectInputStream class helps in deserialization. It converts a byte stream into an object.

Let’s understand the concept using an example. Consider the class Car illustrated below. (Note that it is serializable as it implements the Serializable Interface)

Serialization - Example

A simple code to serialize the class Car using ObjectOutputStream is as below. Here we are creating an object of class Car and later serializing the same into a file named Example.txt

Deserialization - Example

A simple code to deserialize Example.txt file using ObjectInputStream is as below. 

Console output is as below. Observe that the price printed from the deserialized object is 0 and not 18000 as per the original object.

This is because it's marked transient in class Car and hence ignored on serialization. Also, static variable values are not serialized as they belong to the class and not an object.

Serial Version UID

If we notice the class Car we have specified a field named serialVersionUID with some value. It is a universal version identifier for an Serializable class. Deserialization uses this to ensure that the loaded class corresponds to serialized object.

  • Most modern IDEs support generation of serialVersionUID value.

  • If the class doesn’t specify serialVersionUID then the JVM will generate one at run-time.

  • It’s values depends on the structure of the class, its fields, methods, etc.

  • If the class loaded has a different serialVersionUID during deserialization an InvalidClassException is thrown.

While we review and upload more content for you, we would love to hear your feedback.