Serialization and DeSerialization
Serialization and deserialization are processes used to convert objects into a format that can be easily stored, transmitted, or reconstructed, and then back again into objects.
Serialization
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. By using FileOutputStream and ObjectOutputStream classes we can achieve Serialization process.
In Java, serialization is primarily used for:
- Persistence: Saving the state of an object to a file or database.
- Network Communication: Transmitting objects between different Java applications over a network.
- Inter-process Communication: Passing objects between different processes in a distributed system.
DeSerialization
The process of reading state of an object from a file is called “DeSerialization”. But strictly speaking it is the process of converting an object from file supported form (or) network supported form to java supported form. By using FileInputStream and ObjectInputStream classes we can achieve DeSerialization.
package com.ashok.files;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
*
* @author ashok.mariyala
*
*/
class Ashok implements Serializable {
String name = "Ashok";
String website = "https://waytoeasylearn.com";
}
public class MySerialization {
public static void main(String arg[]) throws Exception {
ObjectOutputStream oos = null;
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
Ashok ashok1 = new Ashok();
System.out.println("Serialization started");
FileOutputStream fos = new FileOutputStream("abc.ser");
oos = new ObjectOutputStream(fos);
oos.writeObject(ashok1);
System.out.println("Serialization ended");
System.out.println("Deserialization started");
fis = new FileInputStream("abc.ser");
ois = new ObjectInputStream(fis);
Ashok ashok2 = (Ashok) ois.readObject();
System.out.println("Deserialization ended");
System.out.println(ashok2.name + " ---> " + ashok2.website);
} catch(Exception e) {
e.printStackTrace();
} finally {
if(null != oos) {
oos.close();
}
if(null != fis) {
fis.close();
}
if(null != ois) {
ois.close();
}
}
}
}
Output
Serialization started
Serialization ended
Deserialization started
Deserialization ended
Ashok--->http://waytoeasylearn.com
An Object is said to be serializable if and only if the corresponding class should implement serializable interface. serializable interface present in java.io package and doesn’t contain any method, it is marker interface.
If you are trying to perform serialization of a non serialization Object. we will get run time exception saying NonSerializableException.
If we don’t want to Serialize the value of a particular variable (To meet security constraints) we should declare those variables as transient. While performing serialization JVM ignores the value of transient variables and saves default values instead of original values.
static variables are not part of object state and hence they won’t participate in serialization process. Declaring static variables as transient there is no impact similarly declaring final variables as transient creates no impact.
Serialization in the case of Object Graphs
When ever we are saving an object to a file all the objects which are reachable from that object will be saved by default. This group of objects is called ‘Object Graph’.
In the Object Graph if any Object is non-Serialzable we will get runtime Exception saying NotSerializableException.
package com.ashok.files;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
*
* @author ashok.mariyala
*
*/
class Dog implements Serializable {
Cat c = new Cat();
}
class Cat implements Serializable {
Rat r = new Rat();
}
class Rat implements Serializable {
String name = "Welcome to Waytoeasylearn.com";
}
public class MySerialization {
public static void main(String arg[]) throws Exception {
Dog d = new Dog();
FileOutputStream fos = new FileOutputStream("ashok.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(d);
FileInputStream fis = new FileInputStream("ashok.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Dog d1 = (Dog) ois.readObject();
System.out.println(d1.c.r.j);
}
}
Output
Welcome to Waytoeasylearn.com
In the above program among Dog, Cat and Rat classes if any class is not Serializable we will get runtime Exception saying java.io.NotSerializableException.
Serialization and deserialization provide a convenient way to save and restore object state in Java applications. However, it’s essential to understand its implications and use it appropriately based on the specific requirements of your application.
That’s all about the Serialization and DeSerialization in java. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!