Introduction Of Serialization In Java
Serialization
In Java, a process of writing the state of an object into stream of bytes refers to serialization. It is mostly used in Hibernate, RMI, JPA, EJB, JMS technologies and the reverse operation of serialization is called deserialization.
Java provides an interface (java.io.Serializable) to serialize an object. If we want any class to be serialized , the class needs to implement the given interface.
Serializable interface is a Marker Interface. Hence, there is no method in Serializable Interface.
By default, String class and all the wrapper classes implements java.io.Serializable interface.
Advantage of Serialization
This byte stream can be used for the different purposes like,
- Write to Disk
- Store in Memory
- Sent byte stream to other platform over network
- Save byte stream in DB(As BLOB)
java.io.Serializable interface
Serializable is a marker interface which has no body. It is just used as a marker interface that mark Java classes which support a certain capability. But It should be implemented by the class whose object we want to serialize.
Let's see an example, given below.
Code
- import java.io.Serializable;
- public class Employee implements Serializable {
- int empid;
- String empname;
- int empsalary;
- public Employee(int empid, String empname, int empsalary) {
- this.empid = empid;
- this.empname = empname;
- this.empsalary = empsalary;
- }
- }
ObjectOutputStream class
The ObjectOutputStream class writes the primitive data types and Java objects to an OutputStream. The objects can be read, using an ObjectInputStream. Only objects that support the java.io.Serializable interface can be written to the streams.
Constructor
public ObjectOutputStream(OutputStream out) throws IOException {}
It creates an ObjectOutputStream, which writes to the specified OutputStream.
Methods
public final void writeObject(Object obj) throws IOException {}
This method is used to write the specified object to the ObjectOutputStream.
public void flush() throws IOException {}
This method is used to flush the current output stream.
public void close() throws IOException {}
This method is used to close the current output stream.
Example of serialization.
Code
- import java.io.*;
- public class EmployeeTest {
- public static void main(String args[]) throws Exception {
- Employee e1 = new Employee(101, "Harry", 25000);
- Employee e2 = new Employee(136, "Mia", 30000);
- FileOutputStream fos = new FileOutputStream("Data.txt");
- ObjectOutputStream os = new ObjectOutputStream(fos);
- os.writeObject(e1);
- os.writeObject(e2);
- os.flush();
- os.close();
- fos.close();
- System.out.println("Run successfully....");
- }
- }
Output
In the example, shown above, we serialize the object of Employee class and writeObject() method of ObjectOutputStream class, which is used to serialize the object. We save the state of the object in the file named Data.txt.
Deserialization
In Java, a process of reconstructing the object from the serialized state refers to deserialization. It is the reverse operation of serialization.
ObjectInputStream class
An ObjectInputStream is used to deserialize the objects and primitive data written, using an ObjectOutputStream.
Constructor
public ObjectInputStream(InputStream in) throws IOException {}
It creates an ObjectInputStream, which reads from the specified InputStream.
Methods
public final Object readObject() throws IOException, ClassNotFoundException{}
This method is used to read an object from the input stream.
public void close() throws IOException {}
This method is used to close ObjectInputStream.
Example of Deserialization is given below.
Code
- import java.io.*;
- public class EmployeeTest1 {
- public static void main(String args[]) throws Exception {
- ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Data.txt"));
- Employee e = (Employee) ois.readObject();
- System.out.println(e.empid + " " + e.empname + " " + e.empsalary);
- ois.close();
- }
- }
Output
Serialization with Inheritance
When a class implements serializable then it’s all child classes will also be serializable.
Let's see an example, given below.
Code
- import java.io.Serializable;
- public class Animal implements Serializable {
- String name;
- int age;
- Animal(String name, int age) {
- this.name = name;
- this.age = age;
- }
- }
- public class Dog extends Animal {
- String food;
- public Dog(String name, int age) {
- super(name, age);
- this.food = food;
- }
- }
In the example, shown above, we can serialize the Dog class object, which extends the Animal class that is serializable. Parent class properties are inherited to child classes. Thus, if parent class is Serializable, all child class would also be.
Serialization with Aggregation
When a class has a reference of another class, all the references must be serializable, else serialization process will not be performed and NotSerializableException is thrown at the runtime.
Let’s see an example, given below.
Code
- public class Address {
- String street, town, city, state;
- public Address(String street, String town, String city, String state) {
- this.street = street;
- this.town = town;
- this.city = city;
- this.state = state;
- }
- }
- import java.io.Serializable;
- public class EmployeeEx implements Serializable {
- int empid;
- String empname;
- Address empaddress;
- public EmployeeEx(int id, String name) {
- this.empid = empid;
- this.empname = empname;
- }
- }
In the example, shown above, Address is not serializable and we cannot serialize the object of EmployeeEx class.
Serialization with static data member
Static is the part of a class and not an object due to which it will not be serialized in Java.
Let’s see an example, given below.
Code
- import java.io.Serializable;
- class EmployeeEx implements Serializable {
- int empid;
- String empname;
- static String empCompany = "MNC company";
- public EmployeeEx(int id, String name) {
- this.empid = empid;
- this.empname = empname;
- }
- }
Serialization with array or collection
In Java, all the objects of an array or a collection must be serializable. If any object is not serializable, serialization will be unsuccessful.
Externalizable
The Externalizable interface writes the state of an object into a byte stream in the compressed format and It is not a Marker Interface.
Externalizable interface methods
public void writeExternal(ObjectOutput out) throws IOException
public void readExternal(ObjectInput in) throws IOException
Summary
Thus, we learnt a process of writing the state of an object into the stream of bytes, which refers to serialization and the reverse operation of serialization is called deserialization and we learnt, how to create it in Java.