Java Serialization - Serialization

| Page Views: 44

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.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post