What is Serialization in Java?

Serialization Introduction

Before going into the subject, let us see some code which is very laborious and how Serialization makes it easy.

import java.io.*;
public class Student
  int marks;
  String name;
  public static void main(String args[]) throws IOException
    Student std1 = new Student();

    std1.marks = 50;
    std1.name = "S N Rao";

    DataOutputStream dos = new DataOutputStream(new FileOutputStream("abc.txt"));  



The above code perfectly compiles and runs. The aim of the code is to write the Student data to a hard disk file for a permanent storage so that we can get it any time later (remember, writing to a data structure gives a temporary storage). marks and name are the properties of a Student represented by variables (also known as fields). For two variables, it is required to write two times like writeInt() and writeUTF() using the writing methods of DataOutputStream. If the properties are very many (say around 20) like having night shift allowance, production monthly incentive, attendance bonus etc., it becomes laborious to write twenty write() methods. Yes, it has to be written.

Here comes Serialization to make the job simple (to store all the 20 properties) with a single line of code like using writeObject(std1). But before using writeObject() method with std1, the object std1 must be given a special treatment – the std1 should be serialized.

To explain in a layman type:

Java permits to write objects to a file and retrieve later just like any data types. Question is why should write objects to a file? Writing object to a file makes coding simple. For example, Employee object emp1 has id and name as emp1.id and emp1.name for which values are given. To store emp1.id and emp1.name, it takes two lines of code like dos.writeInt(emp1.id) and dos.writeBytes(emp1.name) where dos is an object of DataOutputStream. But writing an object like writeObject(emp1) takes one line of code. This can be seen more viable when emp1 has twenty properties. But when an object is written to a file and retrieved later, the object may loose the data precision. This is called loosing the persistence. To maintain persistence, Java comes serializing the object.

For example, you go to a bank to have monthly account statement. The server does not send the daily transactions one by one all through the 30 days as it would take long processing on the server, network traffic increases and connection should alive long time. To reduce this, all the transactions are written to a Vector object and the Vector object is sent to the client (bank’s branch) at a time. This is the same case even in Web applications where customer object with lot of selected items should travel between many modules of the business process.

What is Serialization?

Java is a pure object-oriented language (some people argue it is not) where objects are given utmost importance with different behaviours. We know earlier, instead of laborious deep copying the object, the object can be cloned. And now, instead of writing each property one by one separately, whole object can be written at a time. If the whole object is written to a file and later when the object is retrieved, the object should be reconstructed to the original (when retrieved later from the file) state so that the precision of the properties values should not be lost. To achieve this, it is required to serialize the object. Serialization is nothing but giving special treatment to the object and a special request should go to the compiler. See the code.

Or to say simply,

Serialization is the process converting an object into a special stream of bytes so that they can be written to a file or send across distributed network. Serialization gives guarantee that when the object is retrieved back either from the file or network, the object maintains the same properties.

public class Student implements Serializable

When the Student class implements Serializble interface, automatically all the Student objects created are serialized; that is, std1 object is serialized. Serialization is nothing but converting the object into a special stream of bytes before the object is stored. There exists an algorithm with JDK to convert into bytes.

I add some more jargon of words. When the object is serialized, we say the state of the object is preserved. When the state is preserved, the data (attached to the object) becomes persistent (known as data persistence). Persistence literal meaning is long live. The advantage of data persistence is when the serialized object is read back, it is guaranteed to return the same data.

With this knowledge, now let us know what classes exist in Java API to support serialization and then examples on Serialization.

8 thoughts on “What is Serialization in Java?”

  1. Nguyen Viet Quan

    Very easy understanding, I have coverd all the your website, hope you reasle the SpringMVC, Spring Security.. thanks again !!!

  2. Respected sir,
    When an object is written to a file and retrieved later, the object may loose the data precision. This is called loosing the persistence. Sir i want to know the object may loose means? Please can you expalin.

Leave a Comment

Your email address will not be published.