Serialization in JAVA

One of the most interesting corners in ‘Java World’ is serialization. Serialization provides the developer with the ability to store an object’s state in memory, or converting the object to a stream of binary digits, and stored in memory.

Advantages of Serialization

1: Ease of transferring data between objects : In the case of a modern enterprise application, which is spread across various platforms, the need to transmit data among the various components is very critical. In java, where everything is stored as objects, it’s reasonable to transmit data in the form of objects. But for all the platforms to communicate with each other, there should be a common means of communication, a common protocol ( which is nothing but a set of rules ).

In our case, the common protocol is sending data as a stream of bytes. When data is being transferred in the form of objects, the following steps take place in the light of the above figure:
Step 1: The object to be transferred is serialized and stored in the memory of Machine 1.
Step 2: The object stored in the form of a bit stream is transmitted across the network.
Step 3: The Machine 2 receives the bit stream, which is deserialized to get back the object.

2. Freeing up space in the RAM : Now let us see the next advantage of serialization. Making long story short, whenever u feel like your RAM is crowded with a large number of objects and they are eating up your resources, you can serialize some of them to the hard disk. only to deserialize and use them again.
Consider the following example. Suppose, you are running a online store application on your machine where each customer is stored as an object. This object contains all the login details, personal details and the status of the customer’s cart. A single customer object may be so small as a few kilobytes. But in an environment like this, when the number of customers logged in at a moment may go up to several thousands. All of the customers who logged in may not be active. So there is no point in wasting your precious RAM resource for them. So these objects can be serialized to the hard disk, and can be deserialized and used again whenever the particular customer becomes active again.

What is serialized?

In the above discussion, we have not clearly mentioned what part of the object is being serialized. In fact, only the state of the object is being preserved, i.e the values on the variables. Along with this, some metadata on the object itself is being stored. This metadata is used to store some information on the object. Consider the class shown below:



class Match implements Serializable {
public int score = 100;
public int target = 256;
When this class is serialized,

1: The two integer values, viz score and target are converted to bitstream and stored.
2: The metadata about the class is stored. The metadata includes fields such as:
1. The serialization version
2. Length of the class name
3. Number of fields in this class

Also note that, the serialized information can be stored in more than one way. Observe the diagram shown below, it can be plainly to memory, to a file, or to a dedicated database.

Implementing Serialization

Now, let us move on to the most important section, i.e. how to implement serialization.

In order to serialize an object, we have to ensure that the object implements the interface


class Match implements Serializable {
public int score = 100;
public int target = 256;

Serializable Interface and Marker Interface

The Serializable interface is, and empty interface. If u check the code of Serializable interface, you would find something like this.

public interface Serializable

But, dont get fooled by the plain looks. The serializable class does more than what it seems. Serializable is a type of interfce known as marker interface, which do not have any methods, but does the function of tagging a particular object. In this case, the serializable interface tags the object, there by communicating to the JVM that the particualar object can be serialized. An object cannot be serialized until and unless it implements the serializable interface. Other examples of marker interfaces are Clonable, SingleThreadedModel, EventListener etc.

Now that the class or object is eligible for serialization, lets get to the part where we actually serialize it.

See the snippet of code that is employed to serialize the object:

1. FileOutputStream fos = new FileOutputStream("temp.out");
2. ObjectOutputStream oos = new ObjectOutputStream(fos);
3. Match m1 = new Match();
4. oos.writeObject(m1);


The following classes are used here:

FileOutputStream: A FileOutputStream is an OutputStream which is used to write to a file. An OutputStream is a class used to transfer a byte stream. The OutputStream takes in bytes and outputs them to a sink. In the case of FileOutputStream, the sink is a file.

ObjectOutputStream: The ObjectOutputStream is used to write primitive data types and an object to an OutputStream. To make the method of serializable foolproof, only those classes which implements serializable can be written onto an OutputStream.

Now lets see how the code works:

Line 1: A new FileOutputStream instance is created which writes to a file by the name temp.out.
Line 2: A new ObjectOutputStream instance is created which writes the output to the FileOutputStream created in line 1.
Line 3: A new instance of the object to be serialized is created. The same example used above is used here too.
Line 4: The object instantiated in line 3 is written onto the OutputStream, which inturn writes to the file, temp.out. The class of the object, the signature of the class, and the values of the non-transient and non-static fields of the class and all of its supertypes are written.

That is all there to it.

One thought on “Serialization in JAVA

  1. Pingback: A Requiem for Marker Interfaces | Super Compiler

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s