Serialization and Deserialization in Java

What is Serialization?
Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory or to persist objects (e.g. into a file or database).
In terms of Java, Serialization is the porcess of converting a Java object into the form that can be written to a file or network stream.
Serialization in Java is a mechanism of writing the state of an object into a byte stream. It is mainly used in Hibernate, RMI, JPA, EJB and JMS technologies.
The reverse operation of serialization is called deserialization.
Why Serialization?
1. To save data for future use
2. For sendig data to remote computer like RMI or socket programming
3. To send an object between servers in cluster
4. To communicate between applet and servlet
5. To store object in bytes format
Let's consider it by an example.
Let's create a file called Box.java
public class Box {
int height;
int width;
public Box(int height, int width) {
this.height = height;
this.width = width;
}
}
Let's create a file called Main.java
public class Main {
public static void main(String args[]){
Box box = new Box(5, 10);
}
}
When I will run the above code, it will immediately loose the state of student class
once the main class has been executed successfully.
We can not store each and every object in the file or can not pass over the network.
To make the object storable, we have to implement the Serializable
interface.
The states of seriazable's subclass will be stored/persist in a stream.
The stream can be any stream like database, network or a file.
Example of Serialization in Java
Let's make the box serializable.
import java.io.Serializable;
public class Box implements Serializable {
int height;
int width;
public Box(int height, int width) {
this.height = height;
this.width = width;
}
}
Serializable is the marker interface in Java so there is no any other method to implement. In the above example, class Box implements the Serializable so its object's states can be save or persisted.
The object can be written by using the writeObject()
method of ObjectOutputStream
.
Methods of ObjectOutputStream
writeObject | Writes an Object to Object Stream. |
---|---|
close | Closes the stream. |
Let's write the object of Box class in a file called Box.ser
import java.io.*;
public class Main {
public static void main(String args[]) throws Exception {
Box b = new Box(5, 10);
FileOutputStream fout = new FileOutputStream("box.ser");
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(b);
out.flush();
out.close();
fout.close();
}
}
Example of Deserialization in Java
Now let's read the value of Box object that we have recenly stored. Reading the object from file or network back to its normal state is called deserialization.
The object can be read by using the readObject()
method of ObjectInputStream
.
Methods of ObjectInputStream
readObject | Reads an Object from Object Stream. Returns the Obejct class instance. |
---|---|
flush | Flushes the stream. |
close | Closes the stream. |
Let's see how to deserialize an object
import java.io.*;
public class Main {
public static void main(String args[]) throws Exception {
Box b = new Box(5, 10);
FileOutputStream fout = new FileOutputStream("box.ser");
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(b);
out.flush();
out.close();
fout.close();
FileInputStream fin = new FileInputStream("box.ser");
ObjectInputStream oin = new ObjectInputStream(fin);
Box b1 = (Box) oin.readObject();
System.out.println(b1.height);
System.out.println(b1.width);
oin.close();
fin.close();
}
}
Output:
5
10
Here, note that the readObject
method returns the instance of
Object
class which is the super class of all the Classes in Java. So we have to use type casting
mechanism to make it Box type.
Summary
When JVM collects garbage, all the information and states of the objects are lost. You can use Serialization to store the object into a file. The benefit is, till the file is available to you, no matter after how many days, months, years you want to use it, you will be able to get your object back using Deserialization.