Java Serialization Basics

Serialization in Java:

1. What is mean by Serialization

Serializing an object means to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object.

A Java object can be serializable if its class or any of its superclasses implements either the interface or its subinterface


1. Deserialization is the process of converting the serialized form of an object back into a copy of the object.

if we don't want any variable to be part of object's serialized state, we can declare that as transient or static.

NonSerializableException will be thrown if a class doesn't implement Serializable interface. SerialVersionUID maintains version of classes, supposing class might change after serializing an object, then to avoid erros while deserializing.

Externalizable :

1. Externalizable extends Serializable, but unlike Serializable, Externalizable has operations readExternal() and writeExternal() and it is programmers responsibility to implement these methods and explicitly mention which all varialbes needs to be saved, whereas Serializable will serialize all the variables and even variables in all super classes.

Java compiler sees if class implements Externalizable, it will rely totally on writeExternal(), otherwise if Serializale is implemented it will use ObjectOutputStream's writeObject() to save complete state of object unless class overrides the writeObject().