Serialization And Deserialization In Java

Serialization And Deserialization In Java

serialization in Java

Serialization is the process of saving (or) writing state of an object to a file is called serialization but strictly speaking, it is the process of converting an object from java supported form to either network supported form (or) file supported form and is used to save the state of an object to a file or database or to send the object over a network to a different system.

Need Of Serialization

For Example, In distributed systems, messages are often sent between different components of the application. By serializing objects into messages, these components can communicate with each other more easily and efficiently.

And one more example is, Remote method invocation allows a Java program to invoke a method on a remote object running on another machine. Serialization is used to transfer the object across the network and to return results back to the client.

And also used in Network communication allowing us to send an object over a network as a byte stream. This is useful in situations where we need to send data between different computers or processes, such as in a client-server application.

By using FileOutputStream and ObjectOutputStream classes we can achieve the serialization process.

Deserialization in java

Deserialization is a process of reading a state of an object from a file Strictly speaking it is a process of converting an object from file supported or network supported form to java supported form.

By using FileInputStream and ObjectInputStream classes we can achieve Deserialization.

Below is a Diagram for a better understanding:-

Serialization and Deserialization

Below is a simple Java code for serialization and deserialization:-

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
class Person implements Serializable{
int i=10;
int j=20;
}
public class Humans {
public static void main(String[] args)throws
IOException,ClassNotFoundException {
Person p1=new Person();//creating object for person class
System.out.println("serialization started");
FileOutputStream fos= new FileOutputStream("abc.ser");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(p1);
System.out.println("Serialization ended");
System.out.println("Deserialization started");
FileInputStream fis=new FileInputStream("abc.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
Person d2=(Person) ois.readObject();
System.out.println("Deserialization ended");
System.out.println("Person object data");
System.out.println(p2.i+"\t" +p2.j);
}
}

output:-

serialization started

Serialization ended

Deserialization started

Deserialization ended

Person object data

10 20

Note:

  1. We can perform Serialization only for Serializable objects.

  2. An object is said to be Serializable if and only if the corresponding class implements Serializable interface.

  3. Serializable interface is present in the java.io package and does not contain any methods. It is a marker interface. The required ability will be provided automatically by JVM.

  4. If we are trying to serialize a non-serializable object then we will get RuntimeException saying "NotSerializableException".

  5. We can serialize any no of objects to the file but in the same order we need to deserialize, if not it would result in "ClassCastException".