![]() |
VOOZH | about |
Serialization and Deserialization are important Java mechanisms used to convert objects into a byte stream and reconstruct them back into objects. Together, they enable object persistence, data transfer, and communication between different systems while preserving an object's state.
The serialization process converts a Java object into a byte stream, allowing it to be stored or transmitted while preserving its state. Since the generated byte stream is platform-independent, an object serialized on one platform can be deserialized on another platform.
The deserialization process converts a byte stream back into its original Java object, restoring the object's state and data. This allows previously serialized objects to be retrieved and used within the application.
The image below demonstrates the process of serialization and deserialization.
👁 ImageMethod Used for Serialization:
public final void writeObject(Object obj) throws IOException
Method Used for Deserialization:
public final Object readObject() throws IOException, ClassNotFoundException
The Serializable interface is used to make a Java class eligible for serialization. Only objects of classes that implement the java.io.Serializable interface can be serialized and converted into a byte stream.
A marker interface is an interface that does not contain any methods or fields. It is used to provide special information to the JVM or compiler about a class's capabilities.
Example:
class A implements Serializable{
// B also implements Serializable
// interface.
B ob=new B();
}
SerialVersionUID is a unique version identifier for a Serializable class. During deserialization, it ensures that the serialized object and the corresponding class have compatible versions.
Syntax:
private static final long serialVersionUID = 3L;
The serialver tool is provided with the JDK and is used to generate or display the serialVersionUID value of a Serializable class.
We can run the following command to get serialVersionUID serialver [-classpath classpath] [-show] [classname
👁 Image
Example: Serialization and Deserialization of a Java Object.
Output:
Example: Serialization with Transient and Static Fields.
Object has been serialized Data before Deserialization. name = ab age = 20 a = 2 b = 1000 Object has been deserialized Data after Deserialization. name = ab age = 20 a = 0 b = 2000
Explanation In the above code while deserializing the object the values of a and b has changed. The reason being a was marked as transient and b was static.
The main difference between Transient and Final is listed below:
Example:
final int x= 10;
int y = 20;
System.out.println(x);// compiler will replace this as System.out.println(10)->10
because x is final.
System.out.println(y);//20
Example: Demonstration of transient and final behaviour together while serialization.
serialization started Serialization ended Deserialization started Deserialization ended Dog object data 10 20
| Feature | Serialization | Deserialization |
|---|---|---|
| Definition | Process of converting a Java object into a byte stream. | Process of converting a byte stream back into a Java object. |
| Purpose | Used to save or transmit an object's state. | Used to restore and use a previously saved object. |
| Direction | Object -> Byte Stream | Byte Stream -> Object |
| Main Class Used | ObjectOutputStream | ObjectInputStream |
| Method Used | writeObject() | readObject() |
| Output | Serialized byte stream. | Reconstructed Java object. |
| Common Use Cases | File storage, caching, network communication. | Reading stored data, object recovery, data transfer. |
| Exception Handling | May throw IOException. | May throw IOException and ClassNotFoundException. |
| Constructor Invocation | Constructor is used when creating the original object. | Constructor is not called during deserialization. |