Serialization in Java

Serialization

  • Writing objects States into some other sources like Hard Disk, Socket, file etc., is called Serialization.

  • Serialization is needed when there is a problem in sending data from one class to another.
    Where the other class is on a different location or Hard Disk. i.e. in Distributed Systems

  • The reverse operation of serialisation is called deserialization

  • The String Class and all wrapper classes implement serializable interface by default

  • Serializable interface is also marker interface which provides the capability of Serialization to your class. So, we should implement a serializable interface if we want to send the state of an object over the network


Consider the following program of Serialization and Deserialization:
Example 1:


  • The ObjectOutputStream and ObjectInputStream are used to serialize and de-serialize objects respectively.

  • If the superclass implements serializable interface, then all its subclasses will be serializable by default.

  • All static members of class are not serialized because static members are related to class only, not to object.

  • If we don't want to serialize some fields of class then we use the transient keyword. If any member is declared as transient then it won't be serialized.

  • In case of array or collection, all the objects of array or collection must be serializable; if any object is not serializable then the serialization will fail.

  • The serialization associated with each serializable class has a version number called Serial Version UID.

  • It is used during de-serialization to verify that the sender and receiver of a serialized object have loaded classes for that and are compatible with respect to serialization.

  • If the receiver is loaded with different version of a class that has different serial version UIDs than the corresponding sender's class, then de-serialization will result in an invalid Class Exception.

  • A Serializable class can declare its own serial version UID explicitly by declaring a field named serial version UID that must be static, final and of type long.

  • If a superclass variable is made transient, then after de-serialization, it gives default value like zero or null.


Consider the above same program in which we don't want to serialize the age of a student
Example 2:


Externalization is serialization, except that it is an alternative for it.

  • Externalization is nothing but serialization but by implementing Externalizable interface we can persist and restore the object.

  • To externalize your object, you need to implement Externalizable interface that extends the Serializable interface.

  • Here we have complete control of what to serialize and what not to serialize.

  • But with serialization, the identity of all the classes, its superclasses, instance variables and then the contents for these items, is written to the serialization stream.

  • Externalizable interface is not a marker interface and it provides two methods: writeExternal and readExternal.

  • How does serialization happen? JVM first checks for the Externalizable interface and if the object supports Externalizable interface, then it serializes the object using write External method. If the object does not support Externalizable but implements Serializable, then the object is saved using ObjectOutputStream.

  • In case of Serializable, jvm has full control of serializing object, while in case of Externalizable, the application gets the control for persisting objects.

  • writeExternal(), readExternal() methods provides complete control on format and content of serialization process


  • In case of serializable, default serialization process is used. While in case of externalization, custom serialization process is used which is implemented by application.

  • JVM gives a call back to readExternal() and writeExternal of Externalizable interface (application implementation) for restoring and writing objects.

  • Though Externalizable provides complete control, list also has challenges to serialize super type state and take care of default values in case of transient variable and static variables in Java.

  • If used correctly, Externalizable interface can improve the performance of the serialization process.


Example 3:


Example 4: