Serialization and Deserialization in java

The mechanism using which the state of an object gets converted into byte stream is called serialization. Deserialization, on the other hand, is the reverse process, which includes converting the byte stream into a java object in memory. Serialization and deserialization are used to persist the object into memory.

The pictorial representation of Serialization and deserialization is as below:

 


The byte stream created by Serialization is platform-independent which means that the object serialized on one platform can be deserialized on another platform.

To make an object serializable the class should implement Serializable interface. Serializable interface is present in java.io.Serializable package.

This interface is a marker interface. Marker interface means that the interface doesn't contain anything. It only marks the implementing classes to have some special capabilities. In this case, it marks the class to perform Serialization and deserialization of the objects. Cloneable and Remote are the examples of marker interface.


Advantages of serialization

        ·       Serialization is used so that we can save the state of the object.

        ·       It can also be used so that we can transfer the object across the network.


Important points

        ·       No serialization of Static data members.

        ·       No serialization of transient data members.

        ·       During deserialization constructor of the object will never be called.

        ·       All the associate object must implement the Serializable interface.

        ·       If the parent class is implementing the Serializable interface than its child classes will be serialized automatically.

        ·       If the child class is implementing the Serializable interface then the parent the class must implement the Serializable interface.


ObjectInputStream Class

This class is used to serialize an object. There are many write methods in this class. The method which is used frequently is :

public final void writeObject(Object obj) throws IOException

 

The above method sends the object to the output stream by serializing it.


ObjectOutputStream class

This class is used to deserialize an object. Similar to ObjectInputStream, ObjectOutputStream also have method which is used to read/deserialized.

public final Object readObject() throws IOException, ClassNotFoundException

 


SerialVersionUID

During deserialization, to verify that the sender and recipient of the serialized object have loaded classes for that object which is compatible concerning serialization, during serialization at runtime, associates a version number with each serializable class. This serial version is known as serialVersionUID.

While receiving if the receiver has loaded the class for the object which has different serialVersionUID then during deserialization we will get InvalidClassException.

We can also explicitly declare the serialVersionUID. This variable should be static, final and of type long. If the class doesn't declare the UID by itself then based upon different aspects of the class, serialization the runtime will calculate a default UID as described in Java Object Serialization Specifications.

It is strongly recommended that every class should have its UID declared explicitly.

Example:


Output:

Object is serialized

Data before deserialization

Name = Ram

Age = 30

A = 2

B =10

Object is deserialized

Data after deserialization

Name = Ram

Age = 30

A = 0

B =2000














Description of output

As we have seen that during deserialization the value of a and b is changed this is because of the following reasons:

        ·       “a” is declared as transient. The transient variables will not be considered in serialization. The default values will be assigned for example for Objects it will be null and for int, it will be 0.

        ·       “b” is defined as static. Static variables will not be serialized. The default value in the class will be considered.


Further reading:

        1.      Core java interview questions.

        2.      Java 8 interview questions.

        3.      Internal working of Hashmap in java.

        4.      Abstraction in Java.

        5.      Encapsulation in java.

        6.      Compile time polymorphism in java.

        7.      Exception Handling in Java

Post a Comment

0 Comments