Java Serialization - Serialization

Serialization is the process of converting flexible in memory objects to a rigid binary implementation.

Basically you convert an object into a bytestream.

That stream can then be:

  1. Stored in a File.
  2. Sento over the network.
  3. Persisted in the Database.

The reverse process is called Deserialization. It involves taking the bytestream and loading it back into memory as an object. That object can then be programmatically manipulated.

How do you Serialize/Deserialize?

In java to serialize a type you first make it implement interface. This interface was introduced back in JDK 1.0.

Serializable interface is a marker interface. This means that it has neither data members nor methods. It basically marks the interface as having certain capabilities.

Serialization and Deserialization is implemented using two classes defined in thej package.

Class Role Description
ObjectOuputStream Serialize Has a writeObject() method which we can use to serialize an object into a bytestream.
ObjectInputStream Deserialize Has a readObject() method which allows us deserialize our bytestream into an object.

When an object is serialized the result is a sequence of bytes.

This sequence of bytes contains:

  1. Class Name.
  2. Name of non-transient fields(even non-public fields).
  3. Values of these fields.

When an object is deserialized, the sequence of bytes then gets decorded back into a new equivalent instance.

However note that for successful serialization and deserialization you should not change the class names, field names and field types. Doing this can break compatibility between the old and new versions of the class.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:


What do You Think

Previous Post Next Post