Last Updated: 11 October, 2023
Serialization is the process of converting an object into a sequence of bytes that can be persisted to a disk or database or sent through streams over the network.
The reverse process of creating objects from a sequence of bytes is called deserialization.
The entire process of serialization and deserialization is platform-independent, which means we can serialize an object on one platform and deserialize it on another platform (platform means any operation system or hardware).
The process of Java's Serialization-Deserialization is simply depicted in the image given below.
In order to achieve serialization in Java, we must implement java.io.Serializable interface with class Serializable is a marker interface that adds serializable behavior to the class implementing it. For serializing the object,
We call the writeObject() method of java.io.ObjectOutputStream class serializes an object and sends it to the output stream.
Method Declaration Syntax:
public final void writeObject(object x) throws IOException
To perform deserialization, we call the readObject() method of the java.io.ObjectInputStream class.
Method Declaration Syntax:
public final Object readObject() throws IOException, ClassNotFoundException
Serializable is an interface in Java; it's present in the java.io package. Serilizable is a marker interface, which means this interface does not have any methods or fields. Thus, classes implementing it do not have to implement any methods.
The serializable interface is used to serialize (Object -> Byte Stream) or deserialize (Byte Strema -> Object) objects in Java. Serialization is done using java.io.ObjectOutputStream, and deserialization is done using java.io.ObjectInputStream.
All the collection framework classes, String class, and all the wrapper classes implement the java.io.Serializable interface by default.
In serialization, the Java runtime associates a version number with each serializable class. This number is called serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender’s class, then deserialization will result in an InvalidClassException.
A serializable class can declare its own serialVersionUID explicitly by declaring a field named serialVersionUID that must be static, final, and of type long.
serialVersionUID Example:
private static final long serialVersionUID = 2L;
If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class.
Here is a simple example of serialization and deserialization in Java:
Example 1 : Serialization and Deserialization in Java
Output
Serialization Done Successfully...
De-Serialization Done Successfully...
Employee ID : 1001
Employee Name : John
Employee Address : Bangalore
Example 2 : Serializable and Deserialization with 'transient' Variables
Output
Serialization Done Successfully...
De-Serialization Done Successfully...
Name : Kangana
Age : 30
City : null
Pin : null
Example 3 : Serializable and Deserialization with 'static' Variables
Output
Serialization Done Successfully...
De-Serialization Done Successfully...
Name : Karthik
Age : 25
City : Bengaluru
Pin : 560056
While serializing, if we do not want any field to be part of the object state, we can declare it either static or transient based on our needs, and it will not be included during the Java serialization process.
Most impressive is that the entire process is JVM independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform.
When serializing an object to a file, the standard convention in Java is to give the file a .ser extension.
The process of serializing an object is also called deflating or marshalling an object. The opposite operation, extracting a data structure from a series of bytes, is deserialization (which is also called inflating or unmarshalling).
If Serializable interface is not implemented by the class, then writing that class object into a file will lead to a NoSerializableException.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com