What is Serialization? What is Serialization? java java

What is Serialization?


Serialization is the process of turning an object in memory into a stream of bytes so you can do stuff like store it on disk or send it over the network.

Deserialization is the reverse process: turning a stream of bytes into an object in memory.


Simply speaking Serialization is a process of converting an Object into stream of bytes so that it can be transferred over a network or stored in a persistent storage.

Deserialization is the exact opposite - Fetch a stream of bytes from network or persistence storage and convert it back to the Object with the same state.

The thing to understand is how those stream of bytes are interpreted or manipulated so that we get the exact same Object/ same state. There are various ways to achieve that. Some of them are -

  1. XML: Convert Object to XML, transfer it over a network or store it in a file/db. Retrieve it and convert it back to the object with same state. In Java we use JAXB(Java architecture for XML binding) library.(From java 6 it comes bundled with JDK).
  2. JSON: Same can be done by converting the Object to JSON (JavaScript Object notation). Again there is GSON library that can be used for this.
  3. Or we can use the Serialization that is provided by the OOP language itself. For example, in Java you can serialize an Object my making it implement Serializable interface and writing to Object Stream.


What is Serialization?

Simple Explanation via Picture:

Rex, my dog, is serialised!

Summary:

Serialization means transforming something (e.g. my dog Rex) into a series of 1s and 0s - which can be transported over the phone line, stored in memory. My friends overseas can then translate those 1s and 0s back into a perfect representation of a puppy (de-serialization) so they can enjoy Rex's company.

Clarification of Analogy

Friends, this is an analogy. I don't think you can actually serialise a puppy LOL. You would serialise a data structure, or some other complex object. (I wrote this answer so you can understand the concept, in a fun way, in less than 10 seconds, without breaking your head against a technical encyclopedic definition. But if you prefer one please check out: this Wikipedia link).